-
Invitation Cards
- POJ - 1511
- 正反建图
- 跑两边最短路即可。
-
#include<stdio.h> #include<queue> #include<vector> #include<cstring> #include<iostream> using namespace std; #define maxn 8000500 #define ll long long #define inf 0x3f3f3f3f struct node { ll v,w; bool operator <(const node &a)const { return w>a.w; } } dis1[maxn],dis2[maxn]; struct head { int to,ord,w; } edge[maxn]; bool vis[maxn]; ll n,m,t,x,y,z,ans,len,top,he1[maxn],he2[maxn],cnt,temp; void dij(ll *he,node *dis) { dis[1].w=0; priority_queue<node>q; q.push(dis[1]); while(!q.empty()) { top=q.top().v; q.pop(); vis[top]=0; for(int i=he[top]; i!=-1; i=edge[i].ord) { temp=edge[i].to; if(dis[temp].w>dis[top].w+edge[i].w) { dis[temp].w=dis[top].w+edge[i].w; if(!vis[temp]) { vis[temp]=1; q.push(dis[temp]); } } } } } void add(int x,int y,int z) { ++cnt; edge[cnt].ord=he1[x]; edge[cnt].to=y; edge[cnt].w=z; he1[x]=cnt; } void add2(int x,int y,int z) { ++cnt; edge[cnt].ord=he2[x]; edge[cnt].to=y; edge[cnt].w=z; he2[x]=cnt; } int main() { scanf("%lld",&t); while(t--) { ans=cnt=0; scanf("%lld%lld",&n,&m); for(int i=1; i<=n; i++) { dis1[i].w=inf; dis1[i].v=i; dis2[i].w=inf; dis2[i].v=i; vis[i]=0; he1[i]=-1; he2[i]=-1; } while(m--) { scanf("%lld%lld%lld",&x,&y,&z); add(x,y,z); add2(y,x,z); } dij(he1,dis1); dij(he2,dis2); for(int i=2; i<=n; i++) ans+=dis1[i].w+dis2[i].w; printf("%lld\n",ans); } return 0; }
Invitation Cards -向前星建图-堆优化dijkstra
最新推荐文章于 2021-11-02 22:20:32 发布