HDU - 2923 Einbahnstrasse
程序员文章站
2022-04-06 14:01:05
...
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.
InputYour 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: 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. 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 0Sample Output
1. 80
首先我先自掌三下,这题我交了不下于40发,其实第一发的时候我就可以过得,然后因为1<<30超了int的范围就一直改莫名bug,知道改了40发,改到怀疑人生,从网上找代码,然后对代码,改变量名,各种狗血的剧情从我身上上演!
题意很繁琐,这里简单叙述一下吧:
第一行 N,C,R :分别N个城市,C辆坏的车,R条提示
第二行 吊车公司所在城市名称 , C辆坏车所在城市的名称
接下来 有R条提示,即A <-v-- B 表示B可通往A距离是v
A --v-> B 表示A课通往B距离是v
A <-v-> B 表示A和B互通距离是v
注意输入,这一点很重要,根据题意很明显是floyd,直接套就行了!
唯一值得庆祝的是:我学会了map,也算是菜鸟的一点点进步吧!
//hdu 2923
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
#define eps 1e-10
#define maxn 100005
#define zero(a) fabs(a)<eps
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson step<<1
#define rson step<<1|1
#define MOD 1000000009
#define sqr(a) ((a)*(a))
using namespace std;
map <string,int> mat;
int e[105][105];
char s[1050][105];
void init(int n) {
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++) {
if(i == j) e[i][j] = 0;
else e[i][j] = inf;
}
return ;
}
void floyd(int n) {
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(e[i][j] >e[i][k] +e[k][j])
e[i][j] = e[i][k] + e[k][j];
}
}
}
return ;
}
int main() {
int count = 0 , V;
int N, C, R, v, t; //N: 城市个数,C:车辆个数,R:路径个数 ,v表示城市之前的距离,t 表示城市的代号
char c1[105]; //地点名称
char c2[105]; //地点名称
char cl , cr; //cl表示左边的符号,cr表示右边的符号
while(~scanf("%d%d%d",&N,&C,&R)) {
if(N == 0 && C == 0 && R == 0) break;
mat.clear();
V = 0;
count++;
init(N);
t = 1;
for(int i = 0; i <= C; i++) {
scanf("%s",s[i]);
}
for(int i = 0; i < R; i++) {
scanf("%s %c-%d-%c %s",c1,&cl,&v,&cr,c2);
if(!mat[c1]) mat[c1] = t++;
if(!mat[c2]) mat[c2] = t++;
if(cl == '<' && v < e[mat[c2]][mat[c1]])
e[mat[c2]][mat[c1]] = v;
if(cr == '>' && v < e[mat[c1]][mat[c2]])
e[mat[c1]][mat[c2]] = v;
}
floyd(N); // 1 吊车公司所在城市的编号;
for(int i = 1; i <= C; i++) {
V += e[1][mat[s[i]]] + e[mat[s[i]]][1];
}
printf("%d. %d\n",count,V);
}
return 0;
}
Accepted | |
Time | 171ms |
---|---|
Memory | 1884kB |
Length | 1786 |
Lang | G++ |
Submitted | 2017-12-25 16:56:32 |
Shared | |
RemoteRunId | 23392712 |
上一篇: 如何利用php数组对百万数据进行排重
推荐阅读
-
C - Monkey and Banana HDU 1069( 动态规划+叠放长方体)
-
HDU 1052(田忌赛马 贪心)
-
hdu-1338 game predictions(贪心题)
-
致初学者(四):HDU 2044~2050 递推专项习题解
-
C语言BFS--Find a way(Hdu 2612)
-
『ACM C++』HDU杭电OJ | 1425 - sort (排序函数的特殊应用)
-
【hdu5527】【2015ACM/ICPC亚洲区长春站 】Too Rich
-
HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)
-
HDU2196 Computer(树形DP)
-
HDU 5215 Cycle(dfs判环)