#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 3 * 1E4 + 10;
const int maxe = 15 * 1E4 + 10;
int mhead[maxn],nxt[maxe],mend[maxe],mcost[maxe],q;
int vis[maxn],d[maxn];
inline void addedge(int from,int to,int c){
mend[q] = to;
nxt[q] = mhead[from];
mcost[q] = c;
mhead[from] = q++;
}
inline void ini(){
memset(mhead,-1,sizeof(mhead));
q = 2;
}
struct mNode{
int u,c;
mNode(int _u = 0,int _c = 0):u(_u),c(_c){}
bool operator < (const mNode & a)const{
return c > a.c;
}
};
void Dijkstra(int s){
memset(vis,0,sizeof(vis));
memset(d,0x3f,sizeof(d));
priority_queue<mNode> q;
d[s] = 0;
q.push(mNode(s,0));
mNode tmp;
while(!q.empty()){
tmp = q.top();
q.pop();
int u = tmp.u;
if(vis[u]) continue;
vis[u] = 1;
for(int e = mhead[u];~e;e = nxt[e]){
int v = mend[e];
if(!vis[v] && d[v] > d[u] + mcost[e]){
d[v] = d[u] + mcost[e];
q.push(mNode(v,d[v]));
}
}
}
}
int main(){
int n,m,a,b,c;
ini();
scanf("%d%d",&n,&m);
for(int i = 0 ; i < m ; ++i){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
Dijkstra(1);
printf("%d\n",d[n]);
return 0;
}
[2016-04-08][POJ][3159][Candies]
原文:http://www.cnblogs.com/qhy285571052/p/f5da40a5aeeb1b1ab3ae29bf50c9e811.html