poj 1511 Invitation Cards
http://poj.org/problem?id=1511
题意:在一个图中,求从1号节点到2-n号节点的最短距离和和从2-n节点到1节点的最短距离和;
思路:因为数据太大,用spfa,且要建立双向的;
代码:
View Code
#include <iostream> #include <algorithm> #include <cstring> #include <cstdlib> #include <cstdio> using namespace std; #define max 1000010 #define inf 1000000050 struct node { int v; int data; int next; }qian[max],hou[max]; int qq1[max]; int qq2[max]; int n,m; int a,b; int len; long long mm[max]; bool visit[max]; int que[max]; void spfa(node *s,int *link) { for(int i = 0;i <= n;++i) { mm[i] = inf; visit[i] = 0; } mm[1] = 0; visit[1] = 1; que[0] = 1; int front = 0; int rear = 1; int temp; while(front != rear) { temp = que[front]; visit[temp] = 0; front++; if(front == max) front = 0; for(int i = link[temp];i != -1; i = s[i].next) { if(mm[s[i].v] > mm[temp] + s[i].data) { mm[s[i].v] = mm[temp] + s[i].data; if(!visit[s[i].v]) { que[rear++] = s[i].v; if(rear == max) rear = 0; visit[s[i].v] = 1; } } } } } int main() { int w = 0; scanf("%d",&w); while(w--) { scanf("%d%d",&n,&m); fill_n(qq1,max,-1); fill_n(qq2,max,-1); for(int i = 0;i < m; ++i) { scanf("%d%d%d",&a,&b,&len); qian[i].v = a; qian[i].data = len; qian[i].next = qq1[b]; qq1[b] = i; hou[i].v = b; hou[i].data = len; hou[i].next = qq2[a]; qq2[a] = i; } long long ans = 0; spfa(qian,qq1); for(int i = 1;i <= n; ++i) ans = ans + mm[i]; spfa(hou,qq2); for(int i = 1;i <= n; ++i) ans = ans + mm[i]; printf("%lld\n",ans); } return 0; }