牛客2018国庆集训派对Day3 I Metropolis 多源最短路径
传送门:https://www.nowcoder.com/acm/contest/203/I
题意:
求每个大都会到最近的一个大都会的距离。
思路:
把每个大都会设为起点,跑一遍最短路。在跑最短路的时候,假设有一个大都会A,它不能更新一个非大都会B点,就说明这个B到另一个大都会C很近,用$dis[u] + dis[B] + w[u][B]$更新A点的答案。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <complex> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; typedef complex<double> cp; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 // const int mod = 10007; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 2e5+9; ll ans[maxn],dis[maxn]; int star[maxn],from[maxn]; int n,m,q; vector<pll>mp[maxn]; void dji(){ priority_queue<pll>que; for(int i=1; i<=n; i++)dis[i] = inff; for(int i=1; i<=q; i++)dis[star[i]] = 0,que.push(pll(0,star[i])); while(!que.empty()){ pll f = que.top();que.pop(); if(dis[f.se] < -1ll*f.fi)continue; int u = f.se; for(int i=0; i<mp[u].size(); i++){ int v = mp[u][i].fi; if(dis[v] > dis[u] + mp[u][i].se){ dis[v] = dis[u] + mp[u][i].se; from[v] = from[u]; que.push(pll(-1ll*dis[v],v)); } else if(from[v] != from[u]){ ans[from[u]] = min(ans[from[u]] , dis[u] + dis[v] + mp[u][i].se); ans[from[v]] = min(ans[from[v]] , dis[u] + dis[v] + mp[u][i].se); } } } } int main(){ scanf("%d%d%d", &n, &m, &q); for(int i=1; i<=q; i++)scanf("%d",&star[i]),from[star[i]] = star[i]; for(int i=1; i<=n; i++)ans[i] = inff; for(int i=1; i<=m; i++){ int u,v,w; scanf("%d%d%d", &u, &v, &w); mp[u].pb(pll(v,w)); mp[v].pb(pll(u,w)); } dji(); for(int i=1; i<=q; i++){ if(i==q)printf("%lld\n", ans[star[i]]); else printf("%lld ",ans[star[i]]); } return 0; }
skr