Til the Cows Come Home
POJ - 2387
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=2005;
#define inf 0x3f3f3f3f
int t,n;
int dis[maxn];
int vis[maxn];
vector <pair<int,int> >mmp[maxn];
void spfa()
{
for (int i = 1; i<=n; i++)
dis[i]=inf;
queue<int>que;
que.push(1);
vis[1]=1;
dis[1]=0;
while(!que.empty())
{
int top=que.front();
que.pop();
vis[top]=0;
for(int i=0; i<mmp[top].size(); i++)
{
int temp=mmp[top][i].first;
if(dis[top]<inf&&dis[temp]>dis[top]+mmp[top][i].second)
{
dis[temp]=dis[top]+mmp[top][i].second;
if(!vis[temp])
{
que.push(temp);
vis[temp]=1;
}
}
}
}
}
int main()
{
int u,v,w;
memset(vis,0,sizeof(vis));
cin>>t>>n;
while(t--)
{
cin>>u>>v>>w;
mmp[u].push_back(make_pair(v,w));
mmp[v].push_back(make_pair(u,w));
}
spfa();
int ans=dis[n];
cout<<ans<<endl;
return 0;
}