zoukankan      html  css  js  c++  java
  • HDU1595:find the longest of the shortest(spfa+记录路径)

    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 how many minutes it takes to come from one city to another.
    Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
    Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
    Input
    Each case there are two numbers in the first row, N and M, separated by a single space, 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 is 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 road between cities A and B, and that it is crossable in V minutes.
    Output
    In the first line of the 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 10
    2 3 9
    2 4 10
    2 5 1
    3 4 7
    3 5 10
    Sample Output
    11
    13

    27

    题意:一个人从1到n,她要走最短路,但是给的这些路中,有些路坏了,要求在所有可能的最短路中,哪一条最长。原来以为每一条路都遍历一遍然后求最大值,然后就超SI了.......(果然是我too naive。。。)大佬的博客说要把最短路的路径遍历一遍找最大值就好了,所以就是这样的啦,嗯,代码就是这个意思。


    #include<stdio.h>
    #include<iostream>
    #include<cstring>
    #include<string.h>
    #include<algorithm>
    #include<stdlib.h>
    #include<queue>
    #define inf 0x3f3f3f3f
    using namespace std;
    int dis[1005];//记录路的长度的数组
    bool vis[1005];//标记
    int path[1005];//记录路径。
    int n,m,flag;
    struct node
    {
        int u,v,w;
        struct node*next;
    }*h[1005];
    struct nod
    {
        int x,y;
    } d[1000005];//其实这个并没有用,可以直接用三个数字就可以,忘记改了。
    void LA(int u,int v,int w)//连通
    {
        struct node*p=(struct node*)malloc(sizeof(struct node));
        p->v=v;
        p->w=w;
        p->next=h[u];
        h[u]=p;
    }
    void in()//初始化
    {
        for(int i=0; i<=n; i++)
        {
            dis[i]=inf;
        }
    }
    void spfa(int s)
    {
        memset(vis,false,sizeof(vis));
        queue<int>q;
        if(flag)//第一次时要记录路径,后面就不用。
        {
            for(int i=0; i<=n; i++)
            {
                path[i]=-1;
            }
        }
        path[s]=0;
        dis[s]=0;
        vis[s]=true;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            vis[u]=false;
            struct node*p;
            for(p=h[u]; p!=NULL; p=p->next)
            {
                int Q=p->v;
                if(dis[Q]>dis[u]+p->w)
                {
                    dis[Q]=dis[u]+p->w;
                    if(flag)
                    {
                        path[Q]=u;
                    }
                    if(!vis[Q])
                    {
                        vis[Q]=true;
                        q.push(Q);
                    }
                }
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(h,0,sizeof(h));
            memset(d,0,sizeof(d));
            for(int i=0; i<m; i++)
            {
                int l;
                scanf("%d%d%d",&d[i].x,&d[i].y,&l);
                LA(d[i].x,d[i].y,l);
                LA(d[i].y,d[i].x,l);
            }
            in();
            flag=1;
            spfa(1);
            flag=0;
            int s=n;
           int ans=-1;
            while(path[s]>0)
            {
    
                struct node *p1,*q1,*q2,*p2;
                int w;
                for(p1=h[s]; p1!=NULL; p1=p1->next)
                {
                    if(p1->v==path[s])
                    {
                        w=p1->w;
                        q1=p1;
                        p1->w=inf;
                        break;
                    }
                }
                for(p2=h[path[s]]; p2!=NULL; p2=p2->next)
                {
                    if(p2->v==s)
                    {
                        q2=p2;
                        p2->w=inf;
                        break;
                    }
                }
                in();
                spfa(1);
                ans=max(ans,dis[n]);
                q1->w=w;
                q2->w=w;
                s=path[s];
            }
            printf("%d
    ",ans);
        }
    }
    






  • 相关阅读:
    数据挖掘、数据分析的书籍推荐
    跳槽时间如何选择
    求职网站总结
    Eclipse中Applet程序运行时Applet小程序大小的设置
    统计学习导论:基于R应用——第五章习题
    统计学习导论:基于R应用——第四章习题
    统计学习导论:基于R应用——第三章习题
    Windows环境下安装IPython NoteBook
    centos7上mysql无法启动也没有日志
    CentOS 6.4下Squid代理服务器的安装与配置
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053356.html
Copyright © 2011-2022 走看看