HDU5137 How many Maos Does the Guanxi Worth (13 Guangzhou live game K)--Shortest path problem

Source: Internet
Author: User

Link:Click here

Test Instructions: from 2~n-1 These points arbitrarily remove a point, so that the shortest path from 1 to n maximum, if arbitrarily remove a point 1~n no path output INF.

Last year this problem scene is teammate 1 A, feel good nb, at that time will not be the shortest path problem, but the brain probably have such an impression, did the discovery Data compare water, today looked at a bit Dijkstra and Floyd, with two algorithms realized a bit:

The title description is a bit long, but it is easy to figure it out, because the data is really very water, is the shortest short entry training a problem

Dijkstra algorithm
(1) Make s={source point S + has determined the shortest path of the Vertex VI}
(2) for any of the non-included vertex V, define DIST[V] for S to v the most
Short path length, but the path only passes through the vertices in S. That is, the minimum length of the path
(3) If the path is generated in ascending (non-descending) order, the
(4) The true shortest path must only pass through the vertices in s
(5) Each time never included in the vertex selected a dist the smallest ingest (greedy)
(6) Add a V to enter S, which may affect the dist value of another w!

(7) Dist[w] = Min{dist[w], Dist[v] + <v,w> Weight}

Code:

/* Single Source Shortest path problem (Dijkstra), suitable for the case without negative edges, in Bellman-ford algorithm, if D[i] is not the minimum distance, then even the d[j]=d[i]+ (from I to J of the weight of the edge) update, D[J] will not become the shortest distance, And, even though D[i] does not change, each cycle also has to check all the sides from I, which is obviously a waste of time, so the relative Bellman-ford made the following modifications: (1) Find the shortest distance has been determined by the vertex, from his departure to update the shortest path of the adjacent vertices. (2) no longer consider (1) the situation. So how do you find the "vertices with the shortest distance"? At the very beginning, only the shortest distance from the starting point is deterministic, whereas in unused vertices, the smallest vertex of the distance d[i] is the vertex that has been determined because there is no negative edge, so d[i] will not decrease in future updates, which is the origin of the algorithm *//* time:0ms   #include <iostream> #include <algorithm> #include <cstring> #include <cstdio>using                         namespace Std;const int Inf=0x3f3f3f3f;const int maxn_v=35;int v,m;        Vertex number int Cost[maxn_v][maxn_v];                   COST[U][V] denotes the weight of the Edge e= (u,v) int D[maxn_v];               The shortest distance from the vertex s (bool Used[maxn_v];        Already used figure void init ()//Initialize {for (int i=0; i<maxn_v; i++) for (int j=0; j<maxn_v; j + +)            {if (i==j) cost[i][j]=0;        else Cost[i][j]=inf;    }}void input ()//point, side deposit in the graph {int U,v,quan;   for (int i=0; i<m; i++) {     scanf ("%d%d%d", &u,&v,&quan); Cost[u][v]=cost[v][u]=quan; Because it is an}}int graph, the weights are the same as the values of Dijkstra (int s)//find the shortest distance from s to each vertex {for (int i=0; i<maxn_v; i++) D[i]=inf               ;    D[0]=0, other d[i]=inf;    memset (used,false,sizeof (used));    Used[s]=true;    d[1]=0;        while (true) {int v=-1; for (int u=0; u<v; u++)//Select a point with the smallest distance in the unused vertices {if (!used[u] && (v==-1| |        D[U]&LT;D[V]) V=u;        } if (v==-1) break;        Used[v]=true;    for (int u=1; u<=v; u++) d[u]=min (D[u],d[v]+cost[v][u]); } return d[v];}    void Solve () {int ans=-1;        for (int i=2; i<v; i++) {int T=dijkstra (i);            if (T==inf)//does not conform to {printf ("inf\n");        return; } else Ans=max (ans,t); Update} printf ("%d\n", ans);}        int main () {while (~SCANF ("%d%d", &v,&m), v+m) {init ();        Input ();    Solve (); }   return 0;} 

The/*floyd-warshall algorithm solves the problem that the shortest path between all two points is called a short-circuit problem between any two points, and uses the idea of DP to simulate the process: using only the vertices 0~ K and i,j, record I to J is D[k+1][i][j],k=-1, Think that only the use of i,j so d[0][i][j]=cost[i][j];   Next, the problem of using only vertex 0~k is summed up to use only 0~k-1.   When using 0~k only, the shortest path from I to J is discussed by the vertex K and no vertex k problems.    Without the case: D[k][i][j]=d[k-1][i][j], after the case: D[k][i][j]=d[k-1][i][k]+d[k-1][k][j];    Together: D[k][i][j]=min (D[k-1][i][j],d[k-1][i][k]+d[k-1][k][j]); Can be in O (n^3) time complexity to solve all the minimum distance between two points, and compared with the Dijkstra algorithm, Floyd-warshall can handle the situation is negative, and to determine whether there is a negative circle in the graph, just check if there is a d[i][i] is a negative vertex can be. *//*time:15ms  #include <cstdio> #include <cstring> #include <algorithm>using namespace std;                     const int N = 35;const int Inf = 1e8;int V;              Vertex number int m, D[n][n];int g[n][n]; G[U][V] Represents the weight of the Edge e= (u,v) (not present when set to INF, Kee g[i][i]=0;) int solve (int x) {for (int i=1; i<=v; i++) {for (int j=1; J&L T;=v; J + +) {if (i==x| |            J==X) G[i][j]=inf;        else G[i][j]=d[i][j]; }} for (int k=1; k<=v; k++) {for (int i=1, i<=v; i++) {for (int j=1; j<=v; j + +) G[i][j]=min (G[i]        [J],g[i][k]+g[k][j]); }} return g[1][v];}    void input () {for (int i=1, i<=v; i++) {for (int j=1; j<=v; j + +) d[i][j]= (i==j)? 0:inf;        }}void Init () {for (int i=0; i<m; i++) {int U,v,quan;        scanf ("%d%d%d", &u,&v,&quan);        D[u][v]=min (D[u][v],quan);    D[V][U]=D[U][V];    }}void output () {int ans=0;    for (int i=2; i<v; i++) {Ans=max (Ans,solve (i));    } if (Ans==inf) puts ("Inf"); else printf ("%d\n", ans);}        int main () {while (~scanf ("%d%d", &v,&m), v+m) {input ();        Init ();    Output (); } return 0;}


HDU5137 How many Maos Does the Guanxi Worth (13 Guangzhou live game K)--Shortest path problem

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.