这题做的真的很想砸电脑!!!!TLE, 完了WA, 检查完错误又RE,无数次的RE,RE,RE,RE!!!RE你妹啊!把字符串定义在while()外边就能过,定义在里边就RE,还有char型能过,string就TLE,至于卡这么贱吗!!!前后总共来了30多遍!!!!崩溃啊!!!!!!
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
const int N = 108;
const int inf = 0x3fffffff;
map<string, int> m;
int dis[N][N];
int low[N];
bool vis[N];
void Dijkstra(int n, int v, int e)
{
int flag, i, j, min;
for(i = 1;i <= n; i++)
{
low[i] = dis[v][i];
vis[i] = false;
}
vis[v] = true;
low[v] = 0;
for(i = 2; i <= n; i++)
{
min = inf; flag = v;
for(j = 1; j <= n; j++)
{
if(!vis[j] && min > low[j])
{
min = low[j];
flag = j;
}
}
vis[flag] = true;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[flag][j] + low[flag] < low[j])
low[j] = dis[flag][j] + low[flag];
}
}
if(low[e] != inf)
printf("%d\n", low[e]);
else
printf("-1\n");
}
int main()
{
//freopen("data.in", "r", stdin);
int n, num, i, j, x;
char s1[55], s2[55];
char begin[55], end[55];
while(scanf("%d", &n) != EOF)
{
if(n == -1) break;
scanf("%s %s", begin, end);
num = 1;
if(!m[begin])
m[begin] = num++;
if(!m[end])
m[end] = num++;
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
if(i == j)
dis[i][j] = 0;
else
dis[i][j] = inf;
for(i = 1; i <= n; i++)
{
scanf("%s %s %d", s1, s2, &x);
if(!m[s1])
m[s1] = num++;
if(!m[s2])
m[s2] = num++;
dis[m[s1]][m[s2]] = dis[m[s2]][m[s1]] = x;
}
Dijkstra(num-1, m[begin], m[end]);
//cout << m[begin] << " " << m[end] << endl;
m.clear();
}
return 0;
}