Link:
http://acm.hdu.edu.cn/showproblem.php?pid=1595
Topic:
Find the longest of the shortest
Time limit:1000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 667 accepted submission (s): 220
Problem Description
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge. Since She doesn ' t live in the same city, she started preparing for the long journey. We know for every road the minutes it takes to come.
Mirko Overheard in the "the" the "roads is under repairs, and" it is blocked but didn ' t konw exactly Ich road. It is possible to come from Marica "to Mirko ' s" no matter which road is closed.
Marica'll travel only by non-blocked roads, and she would travel by shortest route. Mirko wants to know and how long is it take for her to the the worst of the s girlfriend is out of town for long enough. Write a program so helps Mirko in finding out what are the longest time in minutes it could take for Marica to come by sh Ortest route by the non-blocked roads to he city.
Input
Each case there are two numbers in the "a", N and M, separated by a, the number of Towns,and the number Of roads between the towns. 1≤n≤1000, 1≤m≤n* (N-1)/2. The cities are markedwith numbers from 1 to N, Mirko are located in City 1, and Marica in city N.
In the next M lines are three numbers A, B and V, separated by commas. 1≤a,b≤n, 1≤v≤1000.those numbers mean that there is a two-way the road between cities A and B, and that it is crossable In V minutes.
Output
In the This output file write the maximum time in minutes, it could take Marica to come to Mirko.
Sample Input
5, 6
1 2 4 1 3 3 2 3 1 2 4 4 2 5 7 4 5 1 6 7 1 2 1 2 3 4 3 4 4 4 6 4 1 5
5
2 5 2
5 6
5 5 7 1
2 8 1 4 2 3 9 2 4 2 5 1 3 4 7 3 5 10
Sample Output
27
The main effect of the topic:
There is a city, the city has n locations and m strips connecting their roads, the number of points is from 1 to N, small x lives at 1, he wants to go N.
But the road is being repaired recently, which means that the M road has and only one is bad, but little X does not know which one, a very critical road will increase a lot, so little X want to know from 1 to n * the most * * * * * * * * * * * * * * * * * * *. Can you help him?
Analysis and Summary:
1. The most direct idea is to enumerate each given path, "delete" The path, delete the adjacent matrix corresponding to the edge assignment of the INF, and then find the shortest path in turn.
But all the edges are too many, there is a 1000*999/2 bar, do not think it must be timed out.
So it is most important to choose which edges to delete.
2. Only need to first find the shortest, record a good path, and then enumerate delete this path on the edge, and then find the shortest way, all the most short-circuit the biggest one is the answer. At most, you just need to delete n times. Why just enumerate the shortest side of the road on the line? Very simply, if the enumeration is the other side, then there will be no effect, again the shortest path algorithm, the result is the same as the original shortest path. The way to record a path is to use an array pre record when slack.
Code:
#include <cstring> #include <cstdio> #include <iostream> #include <queue> #include <utility
> Using namespace std;
typedef pair<int,int>pii;
const int INF = 1000000000;
const int VN = 1005;
const int EN = VN*VN/2;
int n, m;
int D[VN];
int W[VN][VN];
int PRE[VN];
BOOL INQ[VN];
BOOL Flag;
void Init () {flag=true;
memset (Pre,-1, sizeof (pre));
for (int i=0; i<=n; ++i) {w[i][i] = INF;
for (int j=i+1; j<=n; ++j) w[i][j] = w[j][i] = INF;
} void Dijkstra (int src) {int TMP_PRE[VN];
memset (Tmp_pre,-1, sizeof (TMP_PRE));
memset (inq, 0, sizeof (INQ));
for (int i=1; i<=n; ++i) D[i]=inf;
D[SRC] = 0;
Priority_queue<pii,vector<pii>,greater<pii> >q;
Q.push (Make_pair (D[SRC],SRC));
while (!q.empty ()) {PII x=q.top (); Q.pop ();
int U=x.second; if (D[u]!=x.first) CONtinue;
for (int v=1; v<=n; ++v) {int tmp=d[u]+w[u][v];
if (W[u][v]!=inf && d[v]>tmp) {Tmp_pre[v] = u;
D[V] = tmp;
Q.push (Make_pair (d[v],v));
}} if (flag) {memcpy (pre, Tmp_pre, sizeof (TMP_PRE));
Flag=false;
int main () {int u,v,c;
while (~SCANF ("%d%d", &n,&m)) {init ();
for (int i=0; i<m; ++i) {scanf ("%d%d%d", &u,&v,&c);
if (w[u][v]>c) w[u][v]=w[v][u]=c;
} Dijkstra (1);
int j=n, Ans=-inf;
while (pre[j]!=-1) {int cost = w[j][pre[j]];//backup W[j][pre[j] = w[pre[j]][j] = INF;
Dijkstra (1);
if (D[n]!=inf && D[n]>ans) ans=d[n];
W[J][PRE[J] = w[pre[j]][j] = Cost;
j = Pre[j]; printf ("%d\n", ans);
return 0; }
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/