HDOJ HDU Today 2112【最短路】

HDU Today

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 19366    Accepted Submission(s): 4552

Problem Description

经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。

这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。

徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?

请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。

Input

输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);

第二行有徐总的所在地start,他的目的地end;

接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。

note:一组数据中地名数不会超过150个。

如果N==-1,表示输入结束。

Output

如果徐总能到达目的地,输出最短的时间;否则,输出“-1”。

Sample Input

6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1

Sample Output

50

Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake

虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。

――全剧终――

Author

lgx

Source

ACM程序设计_期末考试(时间已定!!)

Recommend

lcy   |   We have carefully selected several similar problems for you:  1217 2680 1142 1385 1548

最短路,主要是把字符串处理的过程,题目中说的是无向图。

#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;

const int MAXN = 160;
int d[MAXN];
int cost[MAXN][MAXN];
bool used[MAXN];
int k,max_x;

struct Point{
    char name[50];
    int x;
}point[20020];

void comp(char S[])
{
    strcpy(point[k].name,S);
    for(int i=0;i<k;i++){
        if(strcmp(point[i].name,point[k].name)==0){
            point[k].x=point[i].x;
            return;
        }
    }
    point[k].x=max_x;
    max_x++;
    return;
}

void Dijkstra(int s)
{
    for(int i=0;i<max_x;i++){
        used[i]=false;
        d[i]=INF;
    }
    d[s]=0;
    while(true){
        int v=-1;
        for(int i=0;i<max_x;i++)
            if(!used[i]&&(v==-1||d[v]>d[i])) v=i;
        if(v==-1) break;
        used[v]=true;
        for(int i=0;i<max_x;i++)
            d[i]=min(d[i],d[v]+cost[v][i]);
    }
}

int main()
{
    int N;
    while(scanf("%d",&N),N!=-1){
        Point star,last;
        scanf("%s%s",star.name,last.name);
        star.x=INF;last.x=INF;
        char s1[50];
        int time;
        max_x=0;
        k=0;
        for(int i=0;i<MAXN;i++)
            for(int j=0;j<MAXN;j++)
            cost[i][j]=INF;
        for(int i=0;i<N;i++){
            scanf("%s",s1);
            comp(s1);
            k++;
            scanf("%s",s1);
            comp(s1);
            scanf("%d",&time);
            if(cost[point[k-1].x][point[k].x]>time){
                cost[point[k].x][point[k-1].x]=time;
                cost[point[k-1].x][point[k].x]=time;
            }
            k++;
        }
        int f1,f2;
        f1=f2=0;
        for(int i=0;i<k;i++){
            if(f1&&f2)break;
            if(!f1&&strcmp(point[i].name,star.name)==0){
                star.x=point[i].x;
                f1=1;
            }
            if(!f2&&strcmp(point[i].name,last.name)==0){
                last.x=point[i].x;
                f2=1;
            }
        }
        if(strcmp(last.name,star.name)==0){
            printf("0\n");
            continue;
        }
        if(last.x==INF||star.x==INF){
            printf("-1\n");
            continue;
        }
        Dijkstra(star.x);
        //printf("%d %d\n",star.x,last.x);
        if(d[last.x]==INF)printf("-1\n");
        else printf("%d\n",d[last.x]);
    }
    return 0;
}

HDU Today

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 19366    Accepted Submission(s): 4552

Problem Description

经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。

这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。

徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?

请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。

Input

输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);

第二行有徐总的所在地start,他的目的地end;

接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。

note:一组数据中地名数不会超过150个。

如果N==-1,表示输入结束。

Output

如果徐总能到达目的地,输出最短的时间;否则,输出“-1”。

Sample Input

6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1

Sample Output

50

Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake

虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。

――全剧终――

Author

lgx

Source

ACM程序设计_期末考试(时间已定!!)

Recommend

lcy   |   We have carefully selected several similar problems for you:  1217 2680 1142 1385 1548

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-07-28 19:50:39

HDOJ HDU Today 2112【最短路】的相关文章

hdu 2112 (最短路+map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14515    Accepted Submission(s): 3405 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发

hdu 2112 HDU Today (最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的时候,这里注意不能直接输出0,必须用标记,因为数据可能还没有处理完!!!此处贡献n次wa. (2)这里是某大神教我的用map进行转换,将字符串转换成数字,值得参考.map<string,int>M,V:不过这里同样是wa了好多次.要注意的是将最先输入的开始和结束的点也要放到这个map里面. (3)

HDU 2544:最短路( 最短路径入门 &amp;&amp;Dijkstra &amp;&amp; floyd )

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30972    Accepted Submission(s): 13345 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDOJ/HDU 2140 Michael Scofield&#39;s letter(字符转换~)

Problem Description I believe many people are the fans of prison break. How clever Michael is!! In order that the message won't be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the he

hdu 4568 Hunter 最短路+dp

Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2014    Accepted Submission(s): 615 Problem Description One day, a hunter named James went to a mysterious area to find the treasures. Jame

hdu2112(HDU Today 简单最短路)

Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了.这样住了一段时间,徐总对当地的交通还是不太了解.有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格).徐总经常会问蹩脚的英文问路:“Can

HDOJ/HDU 1161 Eddy&#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

hdu oj 2544 最短路(最短路径)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31874    Accepted Submission(s): 13798 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找