hdu 2112 map+Dijkstra

无向图 用map 起点和终点可能一样 数组不能开太大 WA了好多发

Sample Input
6
xiasha westlake //起点 终点
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1

Sample Output
50

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <algorithm>
# include <cmath>
# include <queue>
# include <map>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=; int n ;
map<string,int> mp ;
bool vis[MAXN];
int cost[MAXN][MAXN] ;
int lowcost[MAXN] ; void Dijkstra(int beg)
{
for(int i=;i<n;i++)
{
lowcost[i]=INF;vis[i]=false;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)
break ;
vis[k]=true;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i]; }
} } int main ()
{
// freopen("in.txt","r",stdin) ;
int m ;
while(cin>>m)
{
if (m == -)
break ;
mp.clear() ;
string s1 ,s2 ;
int i , j , w ;
for (i = ; i < MAXN ; i++)
for (j = ; j < MAXN ; j++)
{
if (i == j)
cost[i][j] = ;
else
cost[i][j] = INF ;
} cin>>s1>>s2 ;
mp[s1] = ;
mp[s2] = ;
n = ;
bool flag = ;
if (s1 == s2)
flag = ;
while(m--)
{
cin>>s1>>s2>>w ;
if (!mp[s1])
mp[s1] = n++ ;
if (!mp[s2])
mp[s2] = n++ ;
if (w < cost[mp[s1]-][mp[s2]-])
{
cost[mp[s1]-][mp[s2]-] = w ;
cost[mp[s2]-][mp[s1]-] = w ;
} }
n-- ;
if (flag)
{
cout<<<<endl ;
continue ;
}
Dijkstra() ; if (lowcost[] != INF)
cout<<lowcost[]<<endl ;
else
cout<<-<<endl ; } return ;
}
上一篇:监听 window.open 打开的窗口关闭并回调


下一篇:SQL Server 外键约束的例子