HDU - 1595——find the longest of the shortest(去边最短路)

本文介绍了一种算法,用于解决给定城市地图中,当某条道路被封锁时,从起点到终点可能面临的最长旅行时间问题。通过使用SPFA算法预处理最短路径,并遍历该路径上的每条边来找出在最坏情况下的最长旅行时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题意:
有一城市,这个城市有n个地点和m条连接他们的路,点的编号是从1到n,小X住在1,他想去n。
但是最近正在维修公路,也就是说这m条路有且只有一条是坏的,但是小X不知道是哪一条,一条很关键的路坏了路程就会增加很多,所以小X想知道从1到n *最*坏*情*况* 下的路程。你能帮助他吗?
思路:

用spfa找到1到n的最短路,枚举每条路径,找出去掉每条路径后的最大值。

#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<queue>
#define inf 0x3f3f3f
#define M 1010
using namespace std;
struct node
{
    int v,w;
    int next;
};
node a[2*M*M];
int first[M],path[2*M*M],pa[2*M*M];
int dis[M],vis[M];
int k;
queue<int>q;
void add(int u,int v,int w)//邻接表
{
    a[k].v=v;
    a[k].w=w;
    a[k].next=first[u];
    first[u]=k++;
}
int spfa(int pan)
{
    int x,l;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[1]=0;
    q.push(1);
    while(!q.empty())
    {
        x=q.front();
        vis[x]=0;
        q.pop();
        l=first[x];
        while(l>=0)
        {
            if(dis[a[l].v]>dis[x]+a[l].w)
            {
                dis[a[l].v]=dis[x]+a[l].w;
                if(pan)//记录路径
                {
                    path[a[l].v]=x;
                    pa[a[l].v]=l;
                }
                if(!vis[a[l].v])
                {
                    vis[a[l].v]=1;
                    q.push(a[l].v);
                }
            }
            l=a[l].next;
        }
    }
}
void init()
{
    memset(first,-1,sizeof(first));
    while(!q.empty())
        q.pop();
}
int main()
{
    int n,m,u,v,w,ans;
    while(~scanf("%d%d",&n,&m))
    {
        ans=0;
        init();
        k=1;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        spfa(1);
        int max_path=inf,sure_path,en=n,l,liu1,liu2;
        while(en!=1)//枚举最短路得每条路径
        {
            l=pa[en];
            l=l+l%2;//因为是双向,处理两条边
            liu1=a[l].w;
            liu2=a[l-1].w;
            a[l].w=inf;
            a[l-1].w=inf;
            spfa(0);
            a[l].w=liu1;
            a[l-1].w=liu2;
            ans=max(ans,dis[n]);
            en=path[en];
        }
        printf("%d\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值