当前位置:网站首页>ABC-Teleporter Setting-(思维+最短路)
ABC-Teleporter Setting-(思维+最短路)
2022-06-27 19:18:00 【可爱美少女】
题意:
就是给你一个图,然后给你边的时候,有可能其中一个点是0,也就是代表这个点是可以变动的。然后对于所有可变动的点,分别让他们为1到n。问每次1到n的最短路是多少。
思考:
刚开始我就想,那些点可以变,如果每次求最短路肯定超时。肯定从1和n分别跑一次最短路,然后再去枚举那些可能会变动的点,距离就是dist1[i]+dist2[j]啥的,但是这样不行,因为有很多点是不确定的,你这样不能保证最短。但是感觉又没啥别的方法可以做了。实际上,还是脑子傻逼了,其实对于那些不确定的点,你就把他当成一个超级源点就行,然后跑完之后。最短路要么就是dist1[n],dist1[0]+dist2[i],dist1[i]+dist2[0]
。因为既然枚举到i的时候,这个0号点就变成了i点,那么就可以让其中一个点到i,另一个到0即可,因为此时0点是i点,所以两个点就会相遇了。
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 3e5+10,M = 2010;
int T,n,m,k;
int va[N];
int dist1[N],dist2[N];
int vis[N];
vector<PII > e[N];
void distra(int A,int dist[])
{
for(int i=0;i<=n;i++) vis[i] = 0;
priority_queue<PII,vector<PII>,greater<PII> > q;
q.push({
0,A});
dist[A] = 0;
while(q.size())
{
auto t = q.top();
q.pop();
int now = t.se,dis = t.fi;
if(vis[now]) continue;
vis[now] = 1;
for(auto tt:e[now])
{
int spot = tt.fi,w = tt.se;
if(dist[spot]>dist[now]+w)
{
dist[spot] = dist[now]+w;
q.push({
dist[spot],spot});
}
}
}
}
signed main()
{
IOS;
cin>>n>>m;
while(m--)
{
int a,b;
cin>>a>>b;
e[a].pb({
b,1});
e[b].pb({
a,1});
}
for(int i=0;i<=n;i++) dist1[i] = dist2[i] = inf;
distra(1,dist1);distra(n,dist2);
for(int i=1;i<=n;i++)
{
int minn = dist1[n];
minn = min(minn,dist1[0]+dist2[i]);
minn = min(minn,dist2[0]+dist1[i]);
if(minn==inf) cout<<"-1 ";
else cout<<minn<<" ";
}
return 0;
}
总结:
多多思考,大胆去尝试。
边栏推荐
- Flexible IP network test tool -- x-launch
- SQL必需掌握的100个重要知识点:用通配符进行过滤
- ARCS模型介绍
- oss上传调用的是哪个方法
- 100 important knowledge points for SQL: in operator
- Full record of 2022 open source moment at Huawei partners and Developers Conference
- 行业案例|从零售之王看银行数字化转型的运营之道
- Leetcode 1381. Design a stack that supports incremental operations
- 强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
- 100 important knowledge points that SQL must master: filtering data
猜你喜欢

GoLand permanently activated

100 important knowledge points that SQL must master: sorting and retrieving data

Model reasoning acceleration based on tensorrt

Modify large online games through CE modifier

Icml2022 | scalable depth Gaussian Markov random field

GFS distributed file system

Full record of 2022 open source moment at Huawei partners and Developers Conference

College graduation thesis management system based on wechat applet graduation design

SQL必需掌握的100个重要知识点:排序检索数据

squid代理服务器
随机推荐
squid代理服务器
Zhongang Mining: the largest application field of new energy or fluorite
MySQL client tools are recommended. I can't imagine that it is best to use Juran
ICML2022 | 可扩展深度高斯马尔可夫随机场
非常全面的DolphinScheduler(海豚调度)安装使用文档
Shuttle hides the return button of the AppBar
划重点!国产电脑上安装字体小技巧
Question brushing notes - tree (easy) - updating
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
爱数课实验 | 第八期-新加坡房价预测模型构建
Use the storcli tool to configure raid. Just collect this article
Is it safe to open an account and buy stocks? Who knows
MySQL客户端工具推荐,一定想不到最好用巨然是它
Unity3D Button根据文本内容自适应大小
GFS分布式文件系统
分享|智慧环保-生态文明信息化解决方案(附PDF)
100 important knowledge points that SQL must master: sorting and retrieving data
VMware vSphere esxi 7.0 installation tutorial
Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!
Flexible IP network test tool -- x-launch