当前位置:网站首页>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;
}
总结:
多多思考,大胆去尝试。
边栏推荐
- Flutter隐藏AppBar的返回按钮
- Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
- Oracle的CTAS能不能将约束等属性带到新表?
- 爱数课实验 | 第六期-金融反欺诈案例研究
- 308. 2D area and retrieval - variable segment tree / hash
- 强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
- SQL必需掌握的100个重要知识点:排序检索数据
- 大促场景下,如何做好网关高可用防护
- Flask----应用案例
- How to do a good job of gateway high availability protection in the big promotion scenario
猜你喜欢

划重点!国产电脑上安装字体小技巧

数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践

Codeforces Global Round 14

Serveur mandataire SQUID

How to participate in openharmony code contribution

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

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

行业案例|从零售之王看银行数字化转型的运营之道

基于微信小程序的高校毕业论文管理系统#毕业设计

Use the storcli tool to configure raid. Just collect this article
随机推荐
划重点!国产电脑上安装字体小技巧
Is it safe to open an account and buy stocks on the Internet? New to stocks, no guidance
Prospects for enterprise digitalization (38/100)
Is it safe to open an account and buy stocks? Who knows
Focus! Tips for installing fonts on domestic computers
ICML2022 | 可扩展深度高斯马尔可夫随机场
2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
How to participate in openharmony code contribution
Share an experience of self positioning + problem solving
银河麒麟系统局域网文件共享教程
100 important knowledge points that SQL must master: filtering data
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
Share how I take notes
爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
VMware vSphere ESXi 7.0安装教程
Experiment of love number lesson | phase V - emotion judgment of commodity review based on machine learning method
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
Graduation design of police report convenience service platform based on wechat applet
MySQL Express - day 1 - basic introduction