Til the Cows Come Home
pku 2387:http://acm.pku.edu.cn/JudgeOnline/problem?id=2387
从n地走到1地,求最小距离。典型的单源最短路径,直接套模板……
-
#include<iostream>
-
#define N 1002
-
#define MAX 99999
-
using namespace std;
-
int edges[N][N],d[N],n;
-
-
void dijkstra(int v)
-
{
-
int i,j;
-
bool s[N]={false};
-
for(i=1;i<=n;i++)
-
d[i]=edges[v][i];
-
d[v]=0;s[v]=true;
-
for(i=1;i<n;i++)
-
{
-
int temp=MAX;
-
int u=v;
-
for(j=1;j<=n;j++)
-
if((!s[j])&&(d[j]<temp))
-
{
-
u=j;
-
temp=d[j];
-
}
-
s[u]=true;
-
for(j=1;j<=n;j++)
-
if((!s[j])&&(edges[u][j]<MAX)&&(d[u]+edges[u][j])<d[j])
-
d[j]=d[u]+edges[u][j];
-
}
-
}
-
-
-
int main(){
-
int t;
-
cin>>t>>n;
-
for(int i=1;i<=n;i++){
-
for(int j=1;j<=n;j++){
-
edges[i][j]=MAX;
-
}
-
}
-
while(t--){
-
int a,b,s;
-
cin>>a>>b>>s;
-
if(edges[a][b]&&s>=edges[a][b])continue;
-
edges[a][b]=edges[b][a]=s;
-
}
-
dijkstra(n);
-
cout<<d[1]<<endl;
-
return 0;
-
}