http://acm.hdu.edu.cn/showproblem.php?pid=2112
裸最短路 主要是练练map STL还是不太熟悉
这题起点可能等于终点 = = 坑了半个多小时
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<map>
using namespace std;
const int maxn = 205;
const int inf = 1<<28;
int n,m;
int maps[maxn][maxn],dis[maxn];
bool vis[maxn];
typedef pair<int,int>pii;
void Dijstra( int x )
{
priority_queue<pii,vector<pii>,greater<pii> >que;
memset( vis,0,sizeof(vis) );
for( int i = 1; i <= n; i ++ )
dis[i] = inf;
dis[x] = 0;
que.push( make_pair(dis[x],x) );
while( !que.empty() )
{
pii u = que.top(); que.pop();
int v = u.second;
if( vis[v] )
continue;
vis[v] = true;
for( int i = 1; i <= n; i ++ )
{
if( maps[v][i] != inf && dis[i] > dis[v] + maps[v][i] )
{
dis[i] = dis[v] + maps[v][i];
que.push( make_pair( dis[i],i ) );
}
}
}
}
int main()
{
//freopen( "data.in","r",stdin );
string s,e,s1,s2;
int a,b,d;
while( scanf("%d",&m) != EOF && m != -1 )
{
map<string,int>bus;
for( int i = 0; i < maxn; i ++ )
{
for( int j = 0; j < maxn; j ++ )
{
if( i == j )
maps[i][j] = 0;
else
maps[i][j] = inf;
}
}
cin>>s>>e;
bus[s] = 1; bus[e] = 2;
int k = 3;
for( int i = 1; i <= m; i ++ )
{
cin>>s1>>s2>>d;
if( !bus[s1] )
{
bus[s1] = k++;
}
if( !bus[s2] )
{
bus[s2] = k++;
}
a = bus[s1]; b = bus[s2];
if( d < maps[a][b] )
maps[a][b] = maps[b][a] = d;
}
if( s == e )
{
puts("0");
continue;
}
n = k-1;
Dijstra(1);
if( dis[2] == inf )
puts("-1");
else
printf("%d\n",dis[2]);
}
return 0;
}