从V到1可以,从1到V会WA,原因分析中
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MAX_V = 1000 + 5;
const int INF = 0xffffff;
struct edge{
int to, cost;
edge(int tt, int cc):to(tt), cost(cc) { }
};
typedef pair<int, int> P;
int V;
vector<edge> G[MAX_V];
int d[MAX_V];
void dijkstra( int s ) {
priority_queue< P, vector<P>, greater<P> > que;
fill(d, d+V, INF);
d[s] = 0;
que.push( P(0,s) );
while( !que.empty() ) {
P p = que.top(); que.pop();
int v = p.second;
if( d[v]<p.first ) { continue; }
for(int i=0; i<G[v].size(); ++i) {
edge e = G[v][i];
if( d[e.to]>d[v]+e.cost ) {
d[e.to] = d[v]+e.cost;
que.push( P(d[e.to],e.to) );
}
}
}
}
int main() {
int T, N;
while( cin >> T >> V ) {
while(T--) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back( edge(b,c) );
G[b].push_back( edge(a,c) );
}
dijkstra( V );
cout << d[1] << endl;
}
return 0;
}