HDU2923 Einbahnstrasse (Floyd)

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=5000+5;
map<string,int>mp;
int n,c,r,w[110][110];
void floyd()
{
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; 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[50],b[50],t[30];
    char d[1010][110],ch2,ch1;
    int k,cas=1,kl;
    while(scanf("%d%d%d",&n,&r,&c)!=EOF)
    {
        k=1;
        if(n==0&&r==0&&c==0) break;
        mp.clear();
        for(int i=0; i<110; i++)
            for(int j=0; j<110; j++)
                if(i==j) w[i][j]=0;
                else w[i][j]=inf;
        for(int i=0; i<=r; i++)
        {
            scanf("%s",&d[i]);
            if(mp[d[i]]==0)
                mp[d[i]]=k++;
        }
        for(int ii=0; ii<c; ii++)
        {
            scanf("%s",&a);
            scanf(" %c-%d-%c ",&ch1,&kl,&ch2);
            scanf("%s",&b);
            if(mp[a]==0) mp[a]=k++;
            if(mp[b]==0) 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=0;
        for(int i=1; i<=r; i++)
        {
            sum+=w[mp[d[0]]][mp[d[i]]]+w[mp[d[i]]][mp[d[0]]];
        }
        printf("%d. %d\n",cas++,sum);
    }

}

时间: 2024-10-08 23:54:01

HDU2923 Einbahnstrasse (Floyd)的相关文章

【UVA】821-Page Hopping(Floyd)

模板题,求一个点到任何一点的距离,用Floyd就行了,结点不一定是从1 ~ n 的,所以需要记录结点的id 14063895 821 Page Hopping Accepted C++ 0.119 2014-08-19 10:00:27 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<sta

hdu 1690 Bus System(Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Problem Description Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it's

人活着系列之开会 (floyd)

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

hdu 1869 (Floyd)

http://acm.hdu.edu.cn/showproblem.php?pid=1869 六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4355    Accepted Submission(s): 1768 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象

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

Floyd算法又称为插点法,是一种用于寻找给定的加权图中多源点之间最短路径的算法. 路径矩阵 通过一个图的权值矩阵求出它的每两点间的最短路径矩阵. 从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归的进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1): 又用同样地公式由D(1)构造出D(2):--:最后又用同样的公式由D(n-1)构造出矩阵D(n).矩阵D(n) 的 i 行 j 列元素便是 i 号顶点到 j 号顶点的最短路径长度,称D(n)为图的距离矩阵,同时还可引入一个后

(floyd)佛洛伊德算法

Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的三重循环,而且纯粹的Floyd算法的循环体内的语句也十分简洁.我认为,正是由于“Floyd算法是一种动态规划(Dynamic Programming)算法”的本质,才导致了Floyd算法如此精妙.因此,这里我将从Floyd算法的状态定义.动态转移方程以及滚动数组等重要方面,来简单剖析一下图论中这一重

【CodeForces - 296D】Greg and Graph(floyd)

Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点之前的所有两点间最短距离和.n<=500 Solution 如果暴力打肯定是会超时的,那就要运用到floyd(hj) floyd算法内2个循环就相当于新加入外循环的那个点然后跟新最短路, 所以可以把题目看成倒过来依次加点,每次\(n^2\)平方更新一下,总共\(O(n^3)\) Code #incl

1035.找出直系亲属(floyd)

题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-. 输入:     输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0<m<50)

hdu1869 六度分离(Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为"小世界现象(small world phenomenon)"的著名假说,大意是说,不论什么2个素不相识的人中间最多仅仅隔着6个人,即仅仅用6个人就能够将他们联系在一起