题目链接:http://acm.hust.edu.cn/vjudge/contest/66569#problem/A
题目大意:给N个点M条边,求1-N的最短路。
解题思路:水题 各种方法应该都能过但我第一遍还是WA了 。哎,要注意1,有重边 2输入先M后N。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxm=2050*2;
int tot;
int n,m;
int head[maxm],next[maxm],ver[maxm],edge[maxm];
int d[1050];
bool vis[1050];
queue<int> q;
void add(int x,int y,int w){
ver[++tot]=y;
edge[tot]=w;
next[tot]=head[x];
head[x]=tot;
}
void spfa(){
memset(d,0x3f,sizeof d);
d[1]=0;vis[1]=1;q.push(1);
while(!q.empty()){
int x=q.front();q.pop();vis[x]=0;
for(int i=head[x];i;i=next[i]){
int y=ver[i];
if(d[y]>d[x]+edge[i]){
d[y]=d[x]+edge[i];
if(!vis[y]){
vis[y]=1;
q.push(y);
}
}
}
}
}
int main(){
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
spfa();
cout<<d[n]<<endl;
return 0;
}