HDU 2923 Einbahnstrasse(最短路 Floyd)

Einbahnstra  e
(German for a one-way street) is a street on which vehicles should only move in one direction. One reason for having one-way streets is to facilitate a smoother flow of traffic through crowded areas. This is useful in city centers, especially old cities like
Cairo and Damascus. Careful planning guarantees that you can get to any location starting from any point. Nevertheless, drivers must carefully plan their route in order to avoid prolonging their trip due to one-way streets. Experienced drivers know that there
are multiple paths to travel between any two locations. Not only that, there might be multiple roads between the same two locations. Knowing the shortest way between any two locations is a must! This is even more important when driving vehicles that are hard
to maneuver (garbage trucks, towing trucks, etc.)

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.

Input

Your program will be tested on one or more test cases. The first line of each test case specifies three numbers (N , C , and R ) separated by one or more spaces. The city has N locations with
distinct names, including the company‘s garage. C is the number of broken cars. R is the number of roads in the city. Note that 0 < N < 100 , 0<=C < 1000 , and R < 10000 . The second line is made of C + 1 words, the first being the location of the company‘s
garage, and the rest being the locations of the broken cars. A location is a word made of 10 letters or less. Letter case is significant. After the second line, there will be exactly R lines, each describing a road. A road is described using one of these three
formats:

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.

Output

For each test case, print the total distance traveled using the following format:

k . V

Where k is test case number (starting at 1,) is a space, and V is the result.

Sample Input

4 2 5
NewTroy Midvale Metrodale
NewTroy   <-20-> Midvale
Midvale   --50-> Bakerline
NewTroy    <-5-- Bakerline
Metrodale <-30-> NewTroy
Metrodale  --5-> Bakerline
0 0 0

Sample Output

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>
时间: 2024-08-05 19:29:38

HDU 2923 Einbahnstrasse(最短路 Floyd)的相关文章

HDU 2923 Einbahnstrasse

英语渣就是这点不好... 巨恶心这种描述多的题.. 大意就是求 从一个点出发,到某些点,然后又从那些点返回的距离之和的 最小值. Dijkstra+邻接矩阵. 正向建图,求出出发距离,然后swap边,求出 返回距离. 注意的是 车可能有重复的.某个点有多少车就需要乘以车的数量. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm&g

HDU 1869 六度分离(最短路 floyd)

六度分离 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人就可以将他们联系在一起,因此他的理论也被称为“六度分离”理论(six degrees of separation).虽然米尔格兰姆的理论屡屡应验,一直也有很多社会学家对其兴趣浓厚,但是在30多年的时间里,它从来就没有得到过严谨的证明,只是一种带有传奇

HDU 2544:最短路( 最短路径入门 &amp;&amp;Dijkstra &amp;&amp; floyd )

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30972    Accepted Submission(s): 13345 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

hdu 2923

最短路加字符串处理 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <cstring> #include <cstdlib> #include <map> using namespace std; #define N 2005 #define INF 0x3f3f3f3f #define LL

SDUT 2930-人活着系列之开会(最短路Floyd)

人活着系列之开会 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 人活着如果是为了事业,从打工的到老板的,个个都在拼搏,奋斗了多年终于有了非凡成就,有了一笔丰富的钱财.反过来说,人若赚取了全世界又有什么益处呢?生不带来,死了你还能带去吗?金钱能买保险,但不能买生命,金钱能买药品,但不能买健康,人生在世,还是虚空呀! 在苍茫的大海上,有很多的小岛,每个人都在自己的小岛上.又到了开会的时候了,鹏哥通过飞信告知了每个人,然后大家就开

【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流

原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队一共有N个据点,据点之间存在M条双向道路.据点分别从1到N标号.小智一行K人从真新镇出发,营救被困在N号据点的皮卡丘.为了方便起见,我们将真新镇视为0号据点,一开始K个人都在0号点. 由于火箭队的重重布防,要想摧毁K号据点,必须

UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)

解题报告 题意: 求所有路中最大分贝最小的路. 思路: 类似floyd算法的思想,u->v可以有另外一点k,通过u->k->v来走,拿u->k和k->v的最大值和u->v比较,存下最小的值. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std;

hdu 2112 HDU Today (最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的时候,这里注意不能直接输出0,必须用标记,因为数据可能还没有处理完!!!此处贡献n次wa. (2)这里是某大神教我的用map进行转换,将字符串转换成数字,值得参考.map<string,int>M,V:不过这里同样是wa了好多次.要注意的是将最先输入的开始和结束的点也要放到这个map里面. (3)

POJ训练计划2253_Frogger(最短路/floyd)

解题报告 题意: 求0到1所有路中最大值最小的那个数. 思路: floyd. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,q; double mmap[210][210]; struct node { double x,y; } p[210]; doub