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. [洛谷P1231] 教辅的组成

    题目大意:有n1本书,n2本练习册和n3个答案,然后又一些条件,说明某本答案可能和某本书对应,某本练习册可能和某本书对应,求最多有多少本完整的书(有书,练习册,答案) 题解:网络流,对应就连边,然后考 ...

  2. C&C++——基本说明

    预处理器(Preprocessor) 1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 60 * ...

  3. 【NOIP 模拟赛】区间第K大(kth) 乱搞

    biubiu~~~ 这道题就是预处理,我们就是枚举每一个数,找到左边比他大的数的个数以及其对应的区间,右边也如此,我们把左边的和右边的相乘就得到了我们的答案,我们发现这是O(n^3)的,但是实际证明他 ...

  4. JavaScript数组遍历map()的原型扩展

    在 JavaScript 1.6 里,javascript 数组增加了几个非常有用的方法:indexOf.lastIndexOf.every. filter. forEach. map. some,其 ...

  5. 编写clearedit的安卓控件

    1.写一个自定义的控件 public class ClearEditText extends AppCompatEditText implements View.OnFocusChangeListen ...

  6. Sqlserver面试题

    1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name   kecheng   fenshu 张三     语文       81张三     数学       75李四     语文   ...

  7. MUI -- plus初始化原理及plus is not defined,mui is not defined 错误汇总

    不要在没有plus和mui的环境下调用相关API 普通浏览器里没有plus环境,只有HBuilder真机运行和打包后才能运行plus api. 在普通浏览器里运行时plus api时控制台必然会输出p ...

  8. 图论:Dinic算法

    解决最大流问题我搜到了一堆的算法:EK算法.FF算法.Dinic算法.SAP算法.ISAP算法 然而并没有什么鸟用 掌握最常见的Dinic就够了,据说极限优化的ISAP比Dinic更快一些..我当不知 ...

  9. 汕头市队赛 SRM 06 C 秀恩爱

    C 秀恩爱 SRM 06 背景&&描述         KPM坐在直升机上俯瞰小渔村景象.         渔村可看作二维平面,密密麻麻地到处都是单身狗,KPM当前所在坐标为(sx,s ...

  10. UVA 10385 Duathlon

    Problem HDuathlonInput: standard inputOutput: standard outputTime Limit: 15 seconds A duathlon is a ...