#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<map>
#include<iostream>
#include<string>
using namespace std;
const int N = 105;
const int inf = 1 << 28;
struct node{
int nxt, to, w;
}e[N*N];
map<string, int> mp;
string str[N*10];
int dis[N], vis[N];
int head[N];
int cnt[N];
int tot;
int n, c, r;
void init()
{
mp.clear();
tot = 0;
memset(head, -1 , sizeof(head));
memset(cnt, 0, sizeof(cnt));
}
void add( int u, int v, int w )
{
e[tot].to = v;
e[tot].w = w;
e[tot].nxt = head[u];
head[u] = tot++;
}
void spfa( int st )
{
queue<int> q;
while( !q.empty() )
q.pop();
q.push(st);
for( int i = 1; i <= n; i++ )
{
dis[i] = inf;
vis[i] = 0;
}
vis[st] = 1;
dis[st] = 0;
while( !q.empty() )
{
int now = q.front();
q.pop();
vis[now] = 0;
for( int i = head[now]; ~i; i = e[i].nxt)
{
int to = e[i].to;
if( dis[to] > dis[now] + e[i].w )
{
dis[to] = dis[now] + e[i].w;
if( !vis[to] )
{
vis[to] = 1;
q.push(to);
}
}
}
}
}
int main()
{
int k = 1;
while(~scanf("%d%d%d", &n, &c, &r) && (n || c || r))
{
init();
char cc[15];
int nx = 0;
for( int i = 1; i <= c+1; i++ )
{
scanf("%s", cc);
str[i] = cc;
if( !mp[cc] )
mp[cc] = ++ nx;
}
for( int i = 1; i <= r; i++ )
{
int st, ed;
int dd = 0;
int dir = 0;
scanf("%s", cc);
//getchar();
if( !mp[cc] )
mp[cc] = ++nx;
st = mp[cc];
char ch;
while( ch = getchar() )
{
if( ch == ' ' )
continue;
else if( ch == '<' )
{
dir = 1;
break;
}
else if( ch == '-' )
break;
}
while( ch = getchar() )
{
if( ch == ' ' )
break;
else if( ch >= '0' && ch <= '9' )
{
dd = dd * 10 + ch - '0';
}
else if( ch == '-' )
continue;
else if(ch == '>' )
{
dir += 2;
break;
}
}
scanf("%s", cc);
if( !mp[cc] )
mp[cc] = ++nx;
ed = mp[cc];
if( dir == 1 )
add(ed, st, dd);
else if( dir == 2 )
add(st, ed, dd);
else
{
add(st, ed, dd);
add(ed, st, dd);
}
}
int ans = 0;
spfa(1);
for( int i = 2; i <= c+1; i++ )
{
ans += dis[mp[str[i]]];
}
//printf("%d\n", ans);
for( int i = 2; i <= c+1; i++ )
{
spfa(mp[str[i]]);
ans += dis[1];
}
printf("%d. %d\n", k++, ans);
}
return 0;
}
最蠢的方法所有的终点跑一遍Embarrassed...
hdu 2923 Einbahnstrasse
最新推荐文章于 2018-06-17 20:22:00 发布