有向图 源点为1 求源点到其他各点的最短距离之和 再在其他点到源点的最短距离之和 再加起来 多源点一终点 只要反向构图就行了
Sample Input
2 //T
2 2 //结点数 边数
1 2 13 //u v w
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
Sample Output
46
210
堆优化: 跑了6S...
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long long 8 using namespace std ; 9 10 const int INF=0x3f3f3f3f; 11 const int MAXN=1000010; 12 struct qnode 13 { 14 int v; 15 int c; 16 qnode(int _v=0,int _c=0):v(_v),c(_c){} 17 bool operator <(const qnode &r)const 18 { 19 return c>r.c; 20 } 21 }; 22 struct Edge 23 { 24 int v,cost; 25 Edge(int _v=0,int _cost=0):v(_v),cost(_cost){} 26 }; 27 vector<Edge>E[MAXN]; 28 bool vis[MAXN]; 29 int dist[MAXN]; 30 int u[MAXN] , v[MAXN] , w[MAXN] ; 31 int n ; 32 void Dijkstra(int start)//点的编号从1开始 33 { 34 memset(vis,false,sizeof(vis)); 35 for(int i=1;i<=n;i++)dist[i]=INF; 36 priority_queue<qnode>que; 37 while(!que.empty())que.pop(); 38 dist[start]=0; 39 que.push(qnode(start,0)); 40 qnode tmp; 41 while(!que.empty()) 42 { 43 tmp=que.top(); 44 que.pop(); 45 int u=tmp.v; 46 if(vis[u])continue; 47 vis[u]=true; 48 for(int i=0;i<E[u].size();i++) 49 { 50 int v=E[tmp.v][i].v; 51 int cost=E[u][i].cost; 52 if(!vis[v]&&dist[v]>dist[u]+cost) 53 { 54 dist[v]=dist[u]+cost; 55 que.push(qnode(v,dist[v])); 56 } 57 } 58 } 59 } 60 void addedge(int u,int v,int w) 61 { 62 E[u].push_back(Edge(v,w)); 63 } 64 65 int main () 66 { 67 // freopen("in.txt","r",stdin) ; 68 int m ; 69 int T ; 70 scanf("%d" , &T) ; 71 while (T--) 72 { 73 scanf("%d %d" , &n , &m) ; 74 LL ans = 0 ; 75 int i , j ; 76 for(i=1;i<=m;i++) 77 scanf("%d%d%d" , &u[i] , &v[i] , &w[i]) ; 78 79 for(i=1;i<=n;i++) 80 E[i].clear(); 81 for(i=1;i<=m;i++) 82 addedge(u[i],v[i],w[i]) ; 83 Dijkstra(1) ; 84 for(i=1;i<=n;i++) 85 ans += dist[i] ; 86 87 for(i=1;i<=n;i++) 88 E[i].clear(); 89 for(i=1;i<=m;i++) 90 addedge(v[i],u[i],w[i]) ; 91 Dijkstra(1) ; 92 for(i=1;i<=n;i++) 93 ans += dist[i] ; 94 95 cout<<ans<<endl ; 96 97 } 98 99 return 0 ; 100 }
时间: 2024-10-14 10:15:03