Einbahnstrasse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3263    Accepted Submission(s): 1018

Problem Description
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
【分析】这个题数据不大,可以用floyd算法,输入的时候注意一下就行了(心血来潮想把之前做的最短路写个题解)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=+;
map<string,int>mp;
int n,c,r,w[][];
void floyd()
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
if(w[i][j]>w[i][k]+w[k][j])
w[i][j]=w[i][k]+w[k][j];
}
}
int main()
{
char a[],b[],t[];
char d[][],ch2,ch1;
int k,cas=,kl;
while(scanf("%d%d%d",&n,&r,&c)!=EOF)
{
k=;
if(n==&&r==&&c==) break;
mp.clear();
for(int i=; i<; i++)
for(int j=; j<; j++)
if(i==j) w[i][j]=;
else w[i][j]=inf;
for(int i=; i<=r; i++)
{
scanf("%s",&d[i]);
if(mp[d[i]]==)
mp[d[i]]=k++;
}
for(int ii=; ii<c; ii++)
{
scanf("%s",&a);
scanf(" %c-%d-%c ",&ch1,&kl,&ch2);
scanf("%s",&b);
if(mp[a]==) mp[a]=k++;
if(mp[b]==) mp[b]=k++;
int u=mp[a],v=mp[b];
if(ch1=='<') w[v][u]=min(kl,w[v][u]);
if(ch2=='>') w[u][v]=min(kl,w[u][v]);
}
floyd();
int sum=;
for(int i=; i<=r; i++)
{
sum+=w[mp[d[]]][mp[d[i]]]+w[mp[d[i]]][mp[d[]]];
}
printf("%d. %d\n",cas++,sum);
} }

HDU2923 Einbahnstrasse (Floyd)的更多相关文章

  1. (floyd)佛洛伊德算法

    Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...

  2. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  3. [CodeForces - 296D]Greg and Graph(floyd)

    Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...

  4. Stockbroker Grapevine(floyd)

    http://poj.org/problem?id=1125 题意: 首先,题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时, 输入数据结束),然后接下来N行描述第i(1< ...

  5. Floyed(floyd)算法详解

    是真懂还是假懂? Floyed算法:是最短路径算法可以说是最慢的一个. 原理:O(n^3)的for循环,对每一个中间节点k做松弛(寻找更短路径): 但它适合算多源最短路径,即任意两点间的距离. 但sp ...

  6. POJ 2253 Frogger(floyd)

    http://poj.org/problem?id=2253 题意 : 题目是说,有这样一只青蛙Freddy,他在一块石头上,他呢注意到青蛙Fiona在另一块石头上,想去拜访,但是两块石头太远了,所以 ...

  7. hdu1869 六度分离(Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 转载请注明出处:http://blog.csdn.net/u012860063?viewmode ...

  8. 最短路(Floyd)-hdu1317

    题目链接:https://vjudge.net/problem/HDU-1317 题目描述: 题意:玩家起始有100个能量点,刚开始在起始房间中,每个房间外有一条单向的路径通往其他房间(一个房间可能通 ...

  9. 算法:最短路径之弗洛伊德(Floyd)算法

    https://cloud.tencent.com/developer/article/1012420 为了能讲明白弗洛伊德(Floyd)算法的主要思想,我们先来看最简单的案例.图7-7-12的左图是 ...

随机推荐

  1. 洛谷 P2480 [SDOI2010]古代猪文 解题报告

    P2480 [SDOI2010]古代猪文 题目背景 "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" ...

  2. HDU.2095(异或运算)

    find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  3. 【CF MEMSQL 3.0 A. Declined Finalists】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Clevo P950系列拆机

    Clevo P950系列(包括神舟精盾T96/T97/T96C/T96E/T97E,炫龙耀9000,铁头人T800同模具机型)拆机 拆机恢复时间:20181203 12:28-14:58   一.普通 ...

  5. 湖南大学第十四届ACM程序设计新生杯 Dandan's lunch

    Dandan's lunch Description: As everyone knows, there are now n people participating in the competiti ...

  6. Codeforces Round #520 (Div. 2) C. Banh-mi

    C. Banh-mi time limit per test:1 second memory limit per test:256 megabytes 题目链接:https://codeforc.es ...

  7. source改变当前路径

    转摘自:http://hi.baidu.com/homappy/item/90e416525d2faf958c12edb7 Shell 脚本执行有三种方法 bash 脚本名 sh 脚本名 chmod ...

  8. Linux 安装编译 FFMPEG

    资源准备: ffmpeg-3.4.tar.bz2 yasm-1.3.0.tar.gz 编译安装: 本人二进制包存放在 /opt/moudles中, 解压缩在 /opt/softwares 解包 ffm ...

  9. WITH AS 使用

    WITH AS 含义: WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让S ...

  10. KVO-基本使用方法-底层原理探究-自定义KVO-对容器类的监听

    书读百变,其义自见! 将KVO形式以代码实现呈现,通俗易懂,更容易掌握 :GitHub   -链接如果失效请自动搜索:https://github.com/henusjj/KVO_base 代码中有详 ...