【记忆化搜索】HDU 1595 find the longest of the shortest

find the longest of the shortest

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 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

题意:输入还是普通的最短路输入,点和权值,其中有一段路径是坏的,但是不知道是那一条路,现在以最短路走,求出最坏的一种情况下的最短路,也就是所有可能性里最长的最短路。

先考虑正常情况下的最短路,断路一定在最短路上才可以影响最终的结果,所以要记住第一次的路径,然后逐个删去求最短路。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define M 0xfffffff;
using namespace std;
int n,m;
int map[1002][1002];
int vis[1002];
int backto[10002],rlist[500002],rlist1[500002];
int e,minm;

void bfs()
{
    int i;
    minm=M;
    e=0;
    memset(vis,-1,sizeof(vis));
    queue<int>q;
    int front;
    front=1;
    vis[1]=0;
    q.push(front);
    memset(backto,0,sizeof(backto));
    while(!q.empty())
    {
        front=q.front();
        q.pop();
        for(i=1; i<=n; i++)
        {
            if(map[front][i]>0&&(vis[front]+map[front][i]<vis[i]||vis[i]<0))
            {
                q.push(i);
                vis[i]=vis[front]+map[front][i];
                if(backto[i]==0)
                e++;
                backto[i]=front;
                if(i==n&&vis[i]<minm)
                {
                    minm=vis[i];
                }
            }
        }
    }
    //printf("minm=%d \n",minm);
    return ;
}

int g;//记录路径数组的长度
void goback()
{
    int j,i,f=e;
    int k=n;
    for(j=e; k!=1; j--)
    {
        f--;
        rlist[j]=backto[k];
        k=backto[k];
    }
    g=0;
    for(i=f+2; i<=e+1; i++)
    {
        rlist1[g]=rlist[i];
        g++;
    }
    rlist1[g-1]=n;
}

int main()
{
    int i,j,k,l;
    while(~scanf("%d%d",&n,&m))
    {
        memset(map,-1,sizeof(map));
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&j,&k,&l);
            if(map[j][k]>=0)
                map[j][k]=min(map[j][k],l);
            else
                map[j][k]=l;
            map[k][j]=map[j][k];
        }
        bfs();
        goback();

        int badr=0;
        for(i=0; i<g; i++)
        {
            j=rlist1[i];
            k=backto[j];
            l=map[k][j];
            map[k][j]=-1;map[j][k]=-1;
            bfs();
            if(minm==0xfffffff)
            {
                badr=-1;
                break;
            }
            if(minm>badr)
            {
                badr=minm;
            }
            map[k][j]=l;map[j][k]=l;
        }
        printf("%d\n",badr);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值