标签:nes ring vol img clu tor tin i++ class
题目链接:http://poj.org/problem?id=1511
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 29286 | Accepted: 9788 |
Description
Input
Output
Sample Input
2 2 2 1 2 13 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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++) 13 #define ms(a,b) memset((a),(b),sizeof((a))) 14 using namespace std; 15 typedef long long LL; 16 const double EPS = 1e-8; 17 const int INF = 2e9; 18 const LL LNF = 9e18; 19 const int MOD = 1e9+7; 20 const int MAXN = 1e6+10; 21 22 int n, m; 23 24 struct edge 25 { 26 int from, to, w, next; 27 }edge[MAXN*4]; 28 int cnt, head[MAXN]; 29 30 void add(int u, int v, int w) 31 { 32 edge[cnt].from = u; 33 edge[cnt].to = v; 34 edge[cnt].w = w; 35 edge[cnt].next = head[u]; 36 head[u] = cnt++; 37 } 38 39 void init() 40 { 41 cnt = 0; 42 memset(head, -1, sizeof(head)); 43 } 44 45 LL dis1[MAXN], dis2[MAXN]; 46 int times[MAXN], inq[MAXN]; 47 void spfa(int st, LL dis[]) 48 { 49 memset(inq, 0, sizeof(inq)); 50 memset(times, 0, sizeof(times)); 51 for(int i = 1; i<=n; i++) 52 dis[i] = LNF; 53 54 queue<int>Q; 55 Q.push(st); 56 inq[st] = 1; 57 dis[st] = 0; 58 while(!Q.empty()) 59 { 60 int u = Q.front(); 61 Q.pop(); inq[u] = 0; 62 for(int i = head[u]; i!=-1; i = edge[i].next) 63 { 64 int v = edge[i].to; 65 if(dis[v]>1LL*dis[u]+1LL*edge[i].w) 66 { 67 dis[v] = 1LL*dis[u]+1LL*edge[i].w; 68 if(!inq[v]) 69 { 70 Q.push(v); 71 inq[v] = 1; 72 } 73 } 74 } 75 } 76 } 77 78 int main() 79 { 80 int T; 81 scanf("%d",&T); 82 while(T--) 83 { 84 init(); 85 scanf("%d%d", &n, &m); 86 for(int i = 0; i<m; i++) 87 { 88 int u, v, w; 89 scanf("%d%d%d", &u, &v, &w); 90 add(u,v,w); 91 } 92 93 spfa(1, dis1); 94 memset(head, -1, sizeof(head)); 95 for(int i = 0; i<m; i++) 96 add(edge[i].to, edge[i].from, edge[i].w); 97 spfa(1, dis2); 98 99 LL ans = 0; 100 for(int i = 1; i<=n; i++) 101 ans += dis1[i]+dis2[i]; 102 103 printf("%lld\n", ans); 104 } 105 }
POJ1511 Invitation Cards —— 最短路spfa
标签:nes ring vol img clu tor tin i++ class
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7610536.html