http://acm.hdu.edu.cn/showproblem.php?pid=2544
直接搞
存份模板
http://blog.csdn.net/niushuai666/article/details/7292722
#include <iostream> #include <cstdio> #include <cstring> #include <string.h> #include<queue> using namespace std; #define maxn 10011 int n,m,first[maxn],u[maxn],v[maxn],w[maxn],next[maxn]; bool inq[maxn]; int i,j,e,d[111]; queue<int> q; int main() { while(1) { scanf("%d%d",&n,&m); if(!n && !m) break; for(i = 1;i <= n;i++) first[i] = -1; for(e = 1;e <= m;e++) { scanf("%d%d%d",&u[e],&v[e],&w[e]); next[e] = first[u[e]]; first[u[e]] = e; } for(i = 1; i <= n;i++) d[i] = (i == 1 ? 0 : 1<<30); for(int k = 1;k <= n;k++) for(i = 1;i <= m;i++) { int x = u[i],y = v[i]; if(d[x] < (1<<30)) if(d[y] > d[x] + w[i]) d[y] = d[x] + w[i]; else if(d[y] < (1<<30)) if(d[x] > d[y] + w[i]) d[x] = d[y] + w[i]; } printf("%d\n",d[n]); } return 0; }