HDU Travel of 20,661 persons (SPFA +floyd-warshall algorithm)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2066

The journey of a man Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 24385 Accepted Submission (s): 8460


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


"The main topic"

Give you some access to the time you need to pass through these pathways.

Then give you some possible starting positions, and possibly some end points.

Find out the shortest time the hero goes to any end. Which destination to go to, if the time is shortest.

The shortest path algorithm with multiple starting points.

Thought one, SPFA, pushes the starting point into the queue. Find the shortest time for each end point. And then find the shortest;


"SPFA"

SPFA Shortest path faster algorithm#include <cstdio> #include <vector> #include <algorithm> #include    <queue> #define INF 0xfffffffconst int maxn = 1010;using namespace std;struct node{int v,len; Node (int v=0,int len=0): V (v), Len (len) {}};int st[1010];int goal[1010];vector<node>g[maxn];int MINDIST[MAXN];    int INQUEUE[MAXN];    int n,m;        int t,s,d;void init () {for (int i=0;i<maxn;i++) {mindist[i]=inf;        inqueue[i]=0;    G[i].clear ();    }}int Dijkstra () {<pre name= "code" class= "CPP" > Queue<int >Q;        for (int i=0;i<s;i++) {//To push all starting points into the queue inqueue[st[i]]=true;        mindist[st[i]]=0;    Q.push (St[i]); } while (!        Q.empty ()) {int vex = Q.front ();        Q.pop ();        inqueue[vex]=0;            for (int i=0;i<g[vex].size (); i++) {int v = G[VEX][I].V;                if (G[vex][i].len+mindist[vex]<mindist[v]) {//slack operation, find shortest edge mindist[v]=g[vex][i].len+mindist[vex]; if (!Inqueue[v]) {inqueue[v]=1;                Q.push (v);    }}}} int Min = INF;    for (int i=0;i<d;i++) {//At the end of the time to find the smallest end point min = min (mindist[goal[i]],min); } printf ("%d\n", Min);}        int main () {while (scanf ("%d%d%d", &t,&s,&d)!=eof) {init ();        int A,b,len;            for (int i=0;i<t;i++) {scanf ("%d%d%d", &a,&b,&len);            G[a].push_back (Node (b,len));        G[b].push_back (Node (a,len));        } for (int i=0;i<s;i++) {scanf ("%d", &st[i]);        } for (int i=0;i<d;i++) {scanf ("%d", &goal[i]);    } Dijkstra (); } return 0;}


"Floyd-warshall algorithm"

Efficiency is O (n^3) used to the idea of DP, not quite understand, but will use, after studying DP back to see it

#include <cstdio> #include <vector> #include <queue> #define INF 0xfffffconst int MAXN = 1010;using namespace Std;int map[maxn][maxn];int st[maxn];int goal[maxn];void init () {for (int. i=1;i<maxn;i++) {for (int J            =1;j<maxn;j++) {if (i==j) map[i][j]=0;        else Map[i][j]=inf;    }}}int Main () {int t,s,d;        while (scanf ("%d%d%d", &t,&s,&d)!=eof) {init ();        int A,b,len;        int min=inf,max=-1;            for (int i=0;i<t;i++) {scanf ("%d%d%d", &a,&b,&len);            if (A>max) Max = A;            if (B>max) Max = b;            if (b<min) Min = b;            if (a<min) Min =a;            if (Len<map[a][b]) {Map[a][b]=map[b][a]=len;  }} for (int k=min;k<=max;k++) {for (int i=min;i<=max;i++) {if (Map[i][k]!=inf)                   Without this optimization it timed out for (int j=min;j<=max;j++) { if (Map[i][j]>map[i][k]+map[k][j]) map[i][j]=map[i][k]+map[k][j];        }}} for (int i=0;i<s;i++) scanf ("%d", &st[i]);        for (int i=0;i<d;i++) scanf ("%d", &goal[i]);        int ans=inf;                    for (int i=0;i<s;i++) {for (int j=0;j<d;j++) {if (Map[st[i]][goal[j]]<ans)            ANS=MAP[ST[I]][GOAL[J]];    }} printf ("%d\n", ans); } return 0;}


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

HDU Travel of 20,661 persons (SPFA +floyd-warshall algorithm)

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.