标签:最短路
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
题意:给出三个数 n, c, r
n 个地点(包括公司的车库),c 表示c辆车抛锚的地点, r 条道路
第二行给出 c+1 个地点,第一个为车库地点, 其余的 c 个为车的地点。
接下来的 r 行表示 r 条有向的道路,
s1 -- d -> s2 表示 s1到s2 的长度为 d
s1 <- d -- s2 表示
s2到s1 的长度为 d
s1 <- d -> s2 表示 s1到s2为双向边, 且长度为 d
拖车从车库出发到每个地点,在该地点拖回抛锚的车子。
一辆拖车一次只能拖回一辆车子。
注意:同一个地点可能有多辆抛锚的车子
求总的路径长度。
还要输出 case number
<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const int MAXN = 1010;
const int INF = 1<<29;
char str[1010][1100];
int dist[110][1100];
char s1[110];
char s2[110];
map<string, int>m;
int n;
void Floyd()
{
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
if(dist[i][j]>dist[i][k]+dist[k][j])
dist[i][j] = dist[i][k]+dist[k][j];
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int c, r;
int num = 1;
while(cin>>n>>c>>r)
{
if(!n && !c && !r)
break;
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= n; j++)
dist[i][j] = (i==j?0:INF);
}
for(int i = 0; i <= c; i++)
{
cin>>str[i];
}
int d, x, y;
char from, to;
int cnt = 1;
m.clear(); // 注意清空,wa 了很多次
for(int i = 0; i < r; i++)
{
scanf("%s %c-%d-%c %s", s1, &from, &d, &to, s2);
if(!m[s1])
m[s1] = cnt++;
if(!m[s2])
m[s2] = cnt++;
x = m[s1];
y = m[s2];
if(from=='<' && d<dist[y][x])
dist[y][x] = d;
if(to=='>' && d<dist[x][y])
dist[x][y] = d;
}
Floyd();
int start = m[str[0]];
int sum = 0;
for(int i = 1; i <= c; i++)
sum += dist[start][m[str[i]]]+dist[m[str[i]]][start];
printf("%d. %d\n", num++, sum);
}
return 0;
}
</span>HDU 2923 Einbahnstrasse(最短路 Floyd)
标签:最短路
原文地址:http://blog.csdn.net/u014028317/article/details/46496795