当前位置:网站首页>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;
}
总结:
多多思考,大胆去尝试。
边栏推荐
- 关于企业数字化的展望(38/100)
- 体验Navicat Premium 16,无限重置试用14天方法(附源码)
- A set of system to reduce 10 times the traffic pressure in crowded areas
- Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
- Modify large online games through CE modifier
- 银河麒麟系统局域网文件共享教程
- GoLand permanently activated
- OpenSSL 编程 一:基本概念
- GoLand永久激活
- Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
猜你喜欢

展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀

Character interception triplets of data warehouse: substrb, substr, substring

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

覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎

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

Save method of JPA stepping pit series

How to participate in openharmony code contribution

麒麟V10安装字体

MySQL performance optimization index function, hidden, prefix, hash index usage (2)

Ceph分布式存储
随机推荐
行业案例|从零售之王看银行数字化转型的运营之道
TypeScript学习
oss上传调用的是哪个方法
使用storcli工具配置RAID,收藏这一篇就够了
Codeforces Round #722 (Div. 2)
100 important knowledge points that SQL must master: creating calculation fields
Squid proxy server
抖音的兴趣电商已经碰到流量天花板?
Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China
MySQL usage notes 1
Full record of 2022 open source moment at Huawei partners and Developers Conference
Model reasoning acceleration based on tensorrt
Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!
MySQL client tools are recommended. I can't imagine that it is best to use Juran
Leetcode 821. Minimum distance of characters (simple) - sequel
Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials
体验Navicat Premium 16,无限重置试用14天方法(附源码)
JPA踩坑系列之save方法
Necessary software tools in embedded software development
如何将队列里面的内容没秒钟执行一次优先级