hdu 2923 floyd+map容器

原创 2016年05月31日 14:57:39


TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 3079    Accepted Submission(s): 960

Problem Description

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

You just started a new job at a car-towing company. The company has a number oftowing trucks parked at the company's garage. A tow-truck lifts the front orback wheels of a broken car in order to pull it straight back to the company'sgarage. You receive calls from various parts of the city about broken cars thatneed to be towed. The cars have to be towed in the same order as you receivethe calls. Your job is to advise the tow-truck drivers regarding the shortestway in order to collect all broken cars back in to the company's garage. At theend of the day, you have to report to the management the total distancetraveled by the trucks.




Your program will be tested on one ormore 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 withdistinct 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, thefirst being the location of the company's garage, and the rest being thelocations 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 Rlines, each describing a road. A road is described using one of these threeformats:

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 formatspecifies a one-way street from location A to B , the second specifies aone-way street from B to A , while the last specifies a two-way street betweenthem. A , ``the arrow", and B are separated by one or more spaces. The endof the test cases is specified with a line having three zeros (for N , C , andR .)

The test case in the example below is the same as the one in the figure.




For each test case, print the totaldistance 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

42 5

NewTroyMidvale Metrodale

NewTroy   <-20-> Midvale

Midvale   --50-> Bakerline

NewTroy    <-5-- Bakerline

Metrodale<-30-> NewTroy

Metrodale  --5-> Bakerline

00 0



Sample Output








分析:从起点到某地,然后在倒过来   求最短路的和。


同样的用map容器  构造一个有向图  就清楚了


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define INF 0x3f3f3f3f
#define MAX 1000
using namespace std;
int vis[MAX],dis[MAX];
int map1[MAX][MAX];
char rr[1001][100];
int n,m,C;
void floyd()
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(map1[i][j] > map1[i][k]+map1[k][j])
                    map1[i][j] = map1[i][k]+map1[k][j];

int main()
    int case1 = 1;
        int ans = 1;
        for(int i = 0; i <= C; i++)
                s[rr[i]] = ans++;
        for(int i = 0; i < 110; i++)
            for(int j = 0; j < 110; j++)
                map1[i][j] = INF;
            map1[i][i] = 0;
        char p[31],q[31];
        char a[31];
        for(int i = 0; i < m; i++)
            if(!s[p]) s[p] = ans++;
            if(!s[q]) s[q] = ans++;
            int len = strlen(a);
            int value = 0,flag = 1;
            for(int j = len - 3; j > 1; j--)
                value+=(a[j] - 48)*flag;
            if(map1[s[p]][s[q]] > value&&a[0] == '<')
                map1[s[p]][s[q]] = value;
            if(map1[s[q]][s[p]] > value&&a[len-1] == '>')
                map1[s[q]][s[p]] = value;
        /**for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                printf("%d ",map1[i][j]);
        printf("%d. ",case1++);
        int sum = 0;
        for(int i = 1; i <= C; i++)
            sum+=map1[s[rr[0]]][s[rr[i]]] + map1[s[rr[i]]][s[rr[0]]];
    return 0;



hdu 2923 Einbahnstrasse 最短路spfa+map容器

题意:从初始点到多个点的来回最短路径总和。 这题输入的时候比较麻烦,注意用map容器存储各点。注意一个目标点可能出现多次。 #include #include #include #include...

hdu 1217 floyd+map容器

Arbitrage TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:65536/32768 K (Java/Others) Total Su...


题意: 给定n个城市,c个目的地(可能有重合的),r条有向边。 接下来是出发点,然后是c个目的地。 接下来r行是路径的信息,箭头代表方向。 求每次从出发点出发到达目的地,然后再回到起点。走c个...

hdu 2923 Einbahnstrasse【floyd】

Einbahnstrasse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...


Problem Description Einbahnstra  e (German for a one-way street) is a street on which vehicles sh...

hdu 2923 Floyd The shortest route

Be Attention ,These is may be a  locality has more than one broken-car .Clearly,Floyd is simple than...
  • NMfloat
  • NMfloat
  • 2015年05月15日 17:05
  • 220

hdu2992 Hotel booking (floyd+spfa+map)

http://acm.hdu.edu.cn/showproblem.php?pid=2992 题意:有一辆车要送东西从1到n,这辆车体力有限只能连续行驶10小时。不过他可以预先让自己的公司给自己...

hdu 2992 Hotel booking(spfa+floyd+map)

http://acm.hdu.edu.cn/showproblem.php?pid=2992 题意:运输公司要从初始城市运送货物到目的城市,共有n个城市,编号是1~n。出发点和目的地分别是1和n号城市...

HDU 2112 HDU Today(floyd-warshall+map)


hdu 2112 HDU Today(map+Floyd)

Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD...
您举报文章:hdu 2923 floyd+map容器