hdoj HDU Today

HDU Today

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

Total Submission(s): 16803    Accepted Submission(s): 3968

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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
char str[155][35];
char s[35],e[35];
int N,st,en;
int map[155][155];
int dist[155],vis[155];
int getnext(int k){
    int u=-1,i,t;
    t=inf;
    for(i=0;i<k;++i){
        if(!vis[i]&&t>dist[i]){
            t=dist[i];u=i;
        }
    }
    return u;
}
int dijkstra(int k){
    int i,j,u=st,temp;
    memset(vis,0,sizeof(vis));
    vis[u]=1;
    while(u!=-1){
        for(i=0;i<k;++i){
            temp=dist[u]+map[u][i];
            if(temp<dist[i])dist[i]=temp;
        }
        vis[u]=1;if(vis[en])return dist[en];
        u=getnext(k);
    }
    return -1;
}
int main()
{
    int i,j,k;
    while(scanf("%d",&N)&&N!=-1){
        k=0;memset(map,0x3f,sizeof(map));
        scanf("%s %s",s,e);
        if(strcmp(s,e)!=0){
            st=k;
            strcpy(str[k++],s);
            en=k;
            strcpy(str[k++],e);
        }
        else {
            st=en=k;strcpy(str[k++],s);
        }
        int c;
        for(i=0;i<N;++i){
            scanf("%s %s %d",s,e,&c);
            int a=-1,b=-1;
            for(j=0;j<k;++j){
                if(strcmp(str[j],s)==0)a=j;
                if(strcmp(str[j],e)==0)b=j;
            }
            if(a==-1){
                a=k;
                strcpy(str[k++],s);
            }
            if(b==-1){
                b=k;
                strcpy(str[k++],e);
            }
            if(map[a][b]>c)map[a][b]=map[b][a]=c;
        }
        memset(dist,0x3f,sizeof(dist));
        dist[st]=0;
        printf("%d\n",dijkstra(k));
    }
    return 0;
}
时间: 2024-10-27 06:54:19

hdoj HDU Today的相关文章

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

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

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2500 做一个正气的杭电人(水~)

Problem Description 做人要有一身正气,杭电学子都应该如此.比如我们今天的考试就应该做到"诚信"为上. 每次考试的第一个题目总是很简单,今天也不例外,本题是要求输出指定大小的"HDU"字符串,特别地,为了体现"正气"二字,我们要求输出的字符串也是正方形的(行数和列数相等). Input 输入的第一行包含一个正整数N(N<=20),表示一共有N组数据,接着是N行数据,每行包含一个正整数M(M<=50),表示一行内有M个

HDOJ/HDU 1062 Text Reverse(字符串翻转~)

Problem Description Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them. Input The input contains several test cases. The first line of the inpu

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强.这时候

HDOJ(HDU) 2153 仙人球的残影(谜一样的题、、、)

Problem Description 在美丽的HDU,有一名大三的同学,他的速度是众所周知的,跑100米仅仅用了2秒47,在他跑步过程中会留下残影的哎,大家很想知道他是谁了吧,他叫仙人球,既然名字这样了,于是他的思想是单一的,他总是喜欢从一点出发,经过3次转折(每次向右转90°),回到出发点,而且呢,他每次转折前总是跑相同长度的路程,所以很多人都想知道如果用'1'算他跑步出发的第一个残影的话,那么回到起点的时候,他的残影是怎么样的呢? Input 测试数据有多行,每一行为一个数N(1<=N<

HDOJ(HDU) 2401 Baskets of Gold Coins(数列、)

Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the