当前位置:网站首页>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;
}
总结:
多多思考,大胆去尝试。
边栏推荐
- KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
- 送你12个常用函数公式,用过的都说好
- 爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
- Question brushing notes - tree (easy) - updating
- Model reasoning acceleration based on tensorrt
- Squid proxy server
- 数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
- 麒麟V10安装字体
- A set of system to reduce 10 times the traffic pressure in crowded areas
- 基于微信小程序的高校党员之家服务管理系统系统小程序#毕业设计,党员,积极分子,学习,打卡,论坛
猜你喜欢

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

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

How to do a good job of gateway high availability protection in the big promotion scenario

“好声音“连唱10年,星空华文如何唱响港交所?

BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!

100 important knowledge points that SQL must master: using functions to process data

Oracle的CTAS能不能将约束等属性带到新表?

DO280OpenShift访问控制--security policy和章节实验

SQL Server for循环用法

Icml2022 | scalable depth Gaussian Markov random field
随机推荐
灵活的IP网络测试工具——— X-Launch
Full record of 2022 open source moment at Huawei partners and Developers Conference
Shell script controls the startup and shutdown of services - with detailed cases
Character interception triplets of data warehouse: substrb, substr, substring
Here are 12 commonly used function formulas for you. All used ones are good
释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
安全高效,非接触“刷手”身份识别助力疫情防控
1030 Travel Plan
行业案例|从零售之王看银行数字化转型的运营之道
# Leetcode 821. Minimum distance of characters (simple)
大促场景下,如何做好网关高可用防护
Can Oracle's CTAs bring constraints and other attributes to the new table?
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China
Codeforces Round #721 (Div. 2)
展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
SQL必需掌握的100个重要知识点:排序检索数据
Is it safe to open an account and buy stocks on the Internet? New to stocks, no guidance
Is it safe to open an account and buy stocks? Who knows