hdoj--2066--a man's journey

Source: Internet
Author: User

The journey of a manTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 24921 Accepted Submission (s): 8664


Problem description Although grass son is a road crazy (is in Hangzhou electric stay for more than a year, incredibly still will be lost in campus, sweat ~), but grass son still very much like travel, because in the journey will meet a lot of people (Prince Charming, ^0^), many things, but also enrich their experience, You can also see the beautiful scenery ... Grass wants to go to many places, she wants to go to the Tokyo Tower to see the night scene, to Venice to see movies, to see the Taro on Yangmingshan, go to New York pure snow, go to Paris to drink coffee letter, to Beijing to visit Meng Jiangnu ... See the winter vacation is coming, so a large period of time, can not be wasted ah, must give yourself good put a fake, but also can not waste training ah, so grass son decided in the shortest time to a place where they want to go! Because the house of grass is in a small town, there is no train passing by, so she can only go to the neighboring city by train (poor Ah ~).
Input data has more than one group, the first line of each group is three integers t,s and D, indicating that there is a T road, and the grass home adjacent to the city of S, the grass wants to go where there are D;
Then there is a T-line, each line has three integer a,b,time, indicating that the drive between A and B cities is time hours; (1=< (A, B) there may be multiple paths between <=1000;a,b)
The next line of T+1 is the number of S, which indicates the city connected with the grass family;
The next line of T+2 has a number D, indicating that the grass wants to go to the place.
Output outputs the shortest time a grass can go to a favorite city.
Sample Input
6 2 31 3 51 4 72 8 123 8 44 9 129 10 21 28 9 10

Sample Output
9
Idea: is single source to find the shortest way, there are n starting point, N focus, to identify each starting point corresponding to the end of the most short-circuit. Compare the output minimum value. With SPFA, with DIJ can also live. AC Code:
#include <stdio.h> #include <string.h> #include <queue> #define INF 0x3f3f3f3f#define N 1010#define M 10010using namespace Std;int head[n],vis[n],dis[n],b[n],e[n],edgenum,t;struct node{int from, To,cost,next;} Edge[m];void init () {memset (head,-1,sizeof (head)); edgenum=0;} void Add (int u,int v,int W) {node e={u,v,w,head[u]};edge[edgenum]=e;head[u]=edgenum++;} void Spfa (int beg) {Queue<int>q;memset (vis,0,sizeof (Vis)); memset (dis,inf,sizeof (dis)); vis[beg]=1;dis[beg]=0 ; Q.push (beg); while (!q.empty ()) {int U=q.front (), I;q.pop (), Vis[u]=0;for (i=head[u];i!=-1;i=edge[i].next) {int V=edge [I].to;if (Dis[v]>dis[u]+edge[i].cost) {dis[v]=dis[u]+edge[i].cost;if (!vis[v]) {Vis[v]=1;q.push (v);}}}} int main () {int beg,end;while (scanf ("%d%d%d", &t,&beg,&end)!=eof) {init (); while (t--) {int a,b,cost;scanf ( "%d%d%d", &a,&b,&cost), add (A,b,cost), add (B,a,cost),//oh, what a fuck turned out to be a bidirectional graph. The whole person is not good. }int i,j,min;for (i=0;i<beg;i++) scanf ("%d", &b[i]), for (i=0;i<end;i++) scanf ("%d", &e[i]); Min=inf;for (i=0;i<beg;i++) {SPFA (b[i]); for (j=0;j<end;j++) if (Min>dis[e[j]]) min=dis[e[j]];} printf ("%d\n", Min);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdoj--2066--a man's journey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.