标签:style blog http color io for div cti log
#include <stdio.h> #include <string.h> #include <queue> #include <stack> #define INF 0x3f3f3f3f using namespace std; struct node { int u,v,w,next; }edge[150001]; int head[30001],dis[30001],vis[30001],k,n,m,s; void add(int u,int v,int w) { edge[k].u = u; edge[k].v = v; edge[k].w = w; edge[k].next = head[u]; head[u] = k++; } void SPFA() { int i; int tt,vv; for(i = 1;i<=n;i++) { dis[i] = INF; vis[i] = 0; } dis[s] = 0; vis[s] = 1; // queue<int>q; stack<int>q; while(!q.empty()) { q.pop(); } q.push(s); while(!q.empty()) { // tt = q.front(); tt = q.top(); q.pop(); vis[tt] = 0; for(i = head[tt];i!=-1;i = edge[i].next) { vv = edge[i].v; if(dis[vv]>dis[tt] + edge[i].w) { dis[vv] = dis[tt] + edge[i].w; if(!vis[vv]) { vis[vv] = 1; q.push(vv); } } } } } int main() { int i,u,v,w; while(scanf("%d%d",&n,&m)!=EOF) { s = 1; for(i = 1;i<=n;i++) { head[i] = -1; } k = 0; for(i = 0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); } SPFA(); printf("%d\n",dis[n]); } return 0; }
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/C
标签:style blog http color io for div cti log
原文地址:http://www.cnblogs.com/yangyongqian/p/3930660.html