Einbahnstrasse
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 5

You just started a new job at a car-towing company. The company has a number of towing trucks parked at the company's garage. A tow-truck lifts the front or back wheels of a broken car in order to pull it straight back to the company's garage. You receive calls from various parts of the city about broken cars that need to be towed. The cars have to be towed in the same order as you receive the calls. Your job is to advise the tow-truck drivers regarding the shortest way in order to collect all broken cars back in to the company's garage. At the end of the day, you have to report to the management the total distance traveled by the trucks.
A -v -> B
A <-v - B
A <-v -> B
A and B are names of two different locations, while v is a positive integer (not exceeding 1000) denoting the length of the road. The first format specifies a one-way street from location A to B , the second specifies a one-way street from B to A , while the last specifies a two-way street between them. A , ``the arrow", and B are separated by one or more spaces. The end of the test cases is specified with a line having three zeros (for N , C , and R .)
The test case in the example below is the same as the one in the figure.

k . V
Where k is test case number (starting at 1,) is a space, and V is the result.
4 2 5 NewTroy Midvale Metrodale NewTroy <-20-> Midvale Midvale --50-> Bakerline NewTroy <-5-- Bakerline Metrodale <-30-> NewTroy Metrodale --5-> Bakerline 0 0 0
1. 80
要记录每个地点的名字,在给出的字符串里判断路的方向,然后floyd
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int n,c,r,m;
int map[101][101];
struct node
{
char city[11];
} cc[101];
char x[11];
char y[11];
int end[1001];
int findCity()
{
for(int i=1; i<=m; i++)
{
if(strcmp(cc[i].city,x)==0)
return i;
}
strcpy(cc[++m].city,x);
return (m);
}
void buildMap()
{
char ch1,ch2,ch3,ch4;
int zz;
for(int i=0; i<r; i++)
{
scanf("%s",x);
int xx=findCity();
cin>>ch1>>ch2>>zz>>ch3>>ch4;
scanf("%s",x);
int yy=findCity();
if(ch1=='<'&&ch4!='>')
{
if(map[yy][xx]>zz)
map[yy][xx]=zz;
}
if(ch1!='<'&&ch4=='>')
{
if(map[xx][yy]>zz)
map[xx][yy]=zz;
}
if(ch1=='<'&&ch4=='>')
{
if(map[yy][xx]>zz)
map[yy][xx]=zz;
if(map[xx][yy]>zz)
map[xx][yy]=zz;
}
}
}
void floyd()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(map[i][j]>map[i][k]+map[k][j])
map[i][j]=map[i][k]+map[k][j];
}
int main()
{
int t=0;
while(scanf("%d%d%d",&n,&c,&r))
{
if(n==0&&c==0&&r==0)
break;
t++;
for(int i=1; i<=n; i++)
{
map[i][i]=0;
for(int j=i+1; j<=n; j++)
map[i][j]=map[j][i]=100000000;
}
m=0;
scanf("%s",x);
int start=findCity();
for(int i=1; i<=c; i++)
{
scanf("%s",x);
end[i]=findCity();
}
buildMap();
floyd();
int sum=0;
for(int i=1; i<=c; i++)
{
sum+=map[end[i]][start];
sum+=map[start][end[i]];
}
printf("%d. %d\n",t,sum);
}
return 0;
}