标签:完整 == ems str color name char std ack
// #include<iostream> #include<cstring> #include<queue> #include<stack> #include<stdio.h> using namespace std; const int INF=0x3f3f3f3f; const int N=30010,M=150010; int h[N],e[M],ne[M],w[M],idx; int st[N]; int dist[N]; int n,m; bool vis[N]; int Scan() { int res = 0, ch, flag = 0; if((ch = getchar()) == ‘-‘) //判断正负 flag = 1; else if(ch >= ‘0‘ && ch <= ‘9‘) //得到完整的数 res = ch - ‘0‘; while((ch = getchar()) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ch - ‘0‘; return flag ? -res : res; } void add(int a,int b,int c) { e[idx]=b; w[idx]=c; ne[idx]=h[a]; h[a]=idx++; } void spfa(int start) { memset(vis,0,sizeof vis); for(int i=0;i<=n;i++) dist[i]=INF; stack<int>s; s.push(start); dist[start]=0; vis[start]=1; while(s.size()) { int u=s.top(); s.pop(); vis[u]=0; for(int j=h[u];j!=-1;j=ne[j]) { int v=e[j]; int w1=w[j]; if(dist[v]>dist[u]+w1) { dist[v]=dist[u]+w1; if(vis[v]==0) { s.push(v); vis[v]=1; } } } } printf("%d\n",dist[n]); } int main() { while(scanf("%d%d", &n, &m)!=EOF) { idx=0; memset(h,-1,sizeof h); for(int i=0;i<m;i++) { int x,y,w; scanf("%d%d%d",&x,&y,&w); add(x,y,w); } spfa(1); } return 0; }
标签:完整 == ems str color name char std ack
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12245519.html