#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
const int INF=0x7fffffff;
using namespace std;
struct stNode{
int p;
int nLen;int cost;
bool operator<(const stNode &x)const{
return (nLen>x.nLen);}
};
vector <stNode> vG[200];
int nD[200],nP[200];
priority_queue<stNode> q;
int nN,nS,nT;
int main(int argc, char** argv) {
int nM;
while (scanf("%d %d",&nN,&nM)!=EOF){
for(int i=0;i<nN;++i){
vG[i].clear();
nD[i]=INF;nP[i]=INF;
}
while (!q.empty())
q.pop();
for(int i=0;i<nM;++i){
int a,b,l,t;
scanf("%d %d %d",&a,&b,&l);
stNode e;
e.p=a;
e.nLen=l;
vG[b].push_back(e);
e.p=b;
vG[a].push_back(e);
}
scanf("%d %d",&nS,&nT);
nD[nS-1]=0;
stNode nd;
nd.p=nS-1;
nd.nLen=0;
q.push(nd);
while(!q.empty()){
stNode t=q.top();
q.pop();
if(t.p==nT-1)
break;
for(int i=0;i<(int)vG[t.p].size();++i){
stNode &e=vG[t.p][i];
if(nD[e.p]>nD[t.p]+e.nLen){
nD[e.p]=nD[t.p]+e.nLen;
nd.p=e.p;
nd.nLen=nD[e.p];
q.push(nd);
}
}
}
if(nD[nT]<INF)
printf("%d\n",nD[nT]);
else
printf("-1\n");
}
return 0;
}

为什么起始点换成1就错了呢。。
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
const int INF=0x7fffffff;
using namespace std;
struct stNode{
int p;
int nLen;int cost;
bool operator<(const stNode &x)const{
return (nLen>x.nLen);}
};
vector <stNode> vG[200];
int nD[200],nP[200];
priority_queue<stNode> q;
int nN,nS,nT;
int main(int argc, char** argv) {
int nM;
while (scanf("%d %d",&nN,&nM)!=EOF){
for(int i=0;i<nN;++i){
vG[i].clear();
nD[i]=INF;nP[i]=INF;
}
while (!q.empty())
q.pop();
for(int i=0;i<nM;++i){
int a,b,l,t;
scanf("%d %d %d",&a,&b,&l);
stNode e;
e.p=a;
e.nLen=l;
vG[b].push_back(e);
e.p=b;
vG[a].push_back(e);
}
scanf("%d %d",&nS,&nT);
nD[nS-1]=0;
stNode nd;
nd.p=nS-1;
nd.nLen=0;
q.push(nd);
while(!q.empty()){
stNode t=q.top();
q.pop();
if(t.p==nT-1)
break;
for(int i=0;i<(int)vG[t.p].size();++i){
stNode &e=vG[t.p][i];
if(nD[e.p]>nD[t.p]+e.nLen){
nD[e.p]=nD[t.p]+e.nLen;
nd.p=e.p;
nd.nLen=nD[e.p];
q.push(nd);
}
}
}
if(nD[nT]<INF)
printf("%d\n",nD[nT]);
else
printf("-1\n");
}
return 0;
}

