HDU 3986 Harry Potter and the Final Battle

Source: Internet
Author: User
Tags bool final integer time limit

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=3986

Topic:

Harry Potter and the Final Battle
Time limit:5000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1139 accepted Submission (s): 359

Problem Description
The final battle is coming. Now Harry Potter are located at City 1, and Voldemort are located at City N. To make is the world peace as soon as possible, of course, Harry Potter'll choose the shortest road City 1 and City N. But Unfortunately, Voldemort is so powerful and can choose to destroy any one of the existing roads as he wish, BU T he can only destroy one. Now given the roads between cities, for are to give of the shortest time of Harry Potter can reach City N and begin the bat Tle in the worst case.

Input
The Case number T (t<=20).
Then for each Case:an integer n (2<=n<=1000) means the number of "magical" world, the cities are from 1 to N. Then An integer m means the roads in the magical World, M (0< m <=50000). Following m lines, with three integers u, V, w (u!= v,1 <=u, v<=n, 1<=w <1000), separated by a Singl E space. It means there is a bidirectional road between U and V with the There May is multiple roads between two cities.

Output
Each case is line:the shortest time of the worst case. If It is impossible to reach City N in the worst case, output "-1".

Sample Input

3
4
4
1 2 5 2 4
ten
1
3 3 3 4 8 3 2 1 2 5 2 3 ten 2 2 1 2 1 1 2 2

Sample Output


1
2

Analysis and Summary:

This question and
HDU 1595 find the longest of the shortest
The same.

But different, the problem is no heavy edge, and this problem has heavy edge, so I do that problem with the adjacency matrix method can not be used again. (If there is a heavy side, the two point between the deletion of a road, there is another way to use, so it is not the same).

So this problem must be done with the Adjacency table method. It's simpler to do it with adjacency tables. Record the shortest path as usual, and note that the path is on the edge. Because it is used adjacency table, is to use "edge" to save, so only need to record this edge in the array position can be, delete the time to make a mark, to this side without relaxing operation can be.

Code:

#include <cstdio> #include <cstring> #include <queue> using namespace std;  
const int INF = 0x7fffffff;  
const int VN = 1010;  
    
const int EN = 50005;   
    struct edge{int v,next,w;  
BOOL used;  
    
}E[EN*2];  
int n,m,size;  
int HEAD[VN];  
int D[VN];  
int PRE[VN];  
int EDGE[VN];  
BOOL Flag;  
    
BOOL INQ[VN];  
    void init () {flag = true;  
    size=0;  
    Memset (Head,-1, sizeof (head));  
memset (Pre,-1, sizeof (pre));  
    } void Addedge (int u,int v,int W) {e[size].v=v;  
    E[size].w=w;  
    E[size].used = true;  
    E[size].next = Head[u];  
Head[u] = size++;  
    } void Spfa (int src) {memset (inq, 0, sizeof (INQ));  
    for (int i=1; i<=n; ++i) d[i] = INF;  
    D[SRC] = 0;  
    queue<int>q;  
    Q.push (SRC);  while (!q.empty ()) {int u = q.front ();  
        Q.pop ();  
        Inq[u] = false; for (int e=head[u]; E!=-1 e=e[e].next) if (e[e].used) {int tmp = D[u] +E[E].W;  
                if (D[E[E].V] > tmp) {D[E[E].V] = tmp;  
                    if (flag) {PRE[E[E].V] = u;  
                EDGE[E[E].V] = E;  
                    } if (!inq[e[e].v]) {INQ[E[E].V] = true;  
                Q.push (E[E].V);  
    int main () {int t,u,v,c;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d%d", &n,&m);  
        Init ();  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d", &u,&v,&c);  
            Addedge (U,V,C);  
        Addedge (V,U,C);  
        } SPFA (1);  
        Flag=false;  
            if (D[n]==inf) {puts ("-1");  
        Continue  
        int ans =-1;  
        int u = n;  
            while (pre[u]!=-1) {e[edge[u]].used = false;  
            SPFA (1); if (d[n]==inf) {ans=-1;  
            Break  
            } if (D[n]>ans) ans=d[n];  
            E[edge[u]].used = true;  
        U = pre[u];  
    printf ("%d\n", ans);  
return 0; }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.