不多说了,就是裸的模版题。
贴代码:
<span style="font-family:KaiTi_GB2312;font-size:18px;">#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 30500 #define M 200000 #define inf 0x3f3f3f3f using namespace std; struct KSD { int v,len,next; }e[M]; int head[N],cnt; void add(int u,int v,int len) { cnt++; e[cnt].v=v; e[cnt].len=len; e[cnt].next=head[u]; head[u]=cnt; } struct Lux { int f,v; bool operator < (const Lux &a)const {return f>a.f;} Lux(){} Lux(int _f,int _v):f(_f),v(_v){} }; int dist[N]; bool in[N]; int pqspfa(int s,int t) { priority_queue<Lux>pq; int i,u,v; memset(dist,0x3f,sizeof(dist)); dist[s]=0; in[s]=1; pq.push(Lux(0,s)); while(!pq.empty()) { Lux U=pq.top();pq.pop(); u=U.v; in[u]=0; for(i=head[u];i;i=e[i].next) { v=e[i].v; if(dist[v]>dist[u]+e[i].len) { dist[v]=dist[u]+e[i].len; if(!in[v]) { in[v]=1; pq.push(Lux(dist[v],v)); } } } } return dist[t]; } int n,m; int main() { // freopen("test.in","r",stdin); int i,j,k; int a,b,c; scanf("%d%d",&n,&m); for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); } printf("%d\n",pqspfa(1,n)); return 0; } </span>
原文地址:http://blog.csdn.net/vmurder/article/details/40724083