hdu 1595 find the longest of the shortest【最短路+枚举】

find the longest of the shortest

Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2743    Accepted Submission(s): 986

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

Author

ailyanlu

 

题目大意:

给出n个点,m条边,一个人想从节点1到节点n旅行,他知道这个图中有一条路正在维修,相当于这条路不能走了,他想估计一下,最坏的情况下,他需要消耗多长时间从节点1到节点n。


思路:

1、题目大意翻译过来就是:在必须删除一条边的条件下,求从节点1到节点n最短路,然后比较一下每一次删除之后的结果,输出最大的那个值,即为所求。

2、最为暴力的想法就是将所有边都枚举一下,依次删除,然后每次都求一次最短路,然后找到最大值,输出,但是很明显,m比较大,直接这样求会超时,那么我们不如这样想一下:假设没有删边情况下的最短路长为mind,如果我们在暴力枚举删边的时候,将不属于这条最短路中的边删除掉之后,再求一遍最短路的值无疑还是mind,所以我们要删除的边,一定是属于没有删边时候求的最短路中的边。

3、思路框架构建完毕,我们直接建图,然后在松弛过程中记录路径,然后将路径上的边依次枚举出来,删除,再求一遍最短路,然后维护最大值,输出即可。


代码实现问题相关:

1、SPFA+邻接矩阵实现(毕竟链式前向星(静态邻接表)实现的时候删边不方便,所以采用了邻接矩阵的构图方式♪(^∇^*)),G++TLE,C++ 4100+msAC。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[1005][1005];
int path[1005];
int dis[1005];
int vis[1005];
int ans[100000][2];
int n,m,guanjian;
void init()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            map[i][j]=0x3f3f3f3f;
        }
    }
}
void SPFA(int x)
{
    memset(vis,0,sizeof(vis));
    memset(path,-1,sizeof(path));
    for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
    queue<int >s;
    s.push(x);
    vis[x]=1;
    dis[x]=0;
    while(!s.empty())
    {
        int u=s.front();
        s.pop();vis[u]=0;
        for(int i=1;i<=n;i++)
        {
            int v=i;
            if(dis[v]>dis[u]+map[u][v])
            {
                dis[v]=dis[u]+map[u][v];
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
                path[v]=u;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            if(map[x][y]>w)
            {
                map[x][y]=map[y][x]=w;
            }
        }
        SPFA(1);
        int cont=0;
        int v=n;
        while(1)
        {
            ans[cont][1]=v;
            ans[cont++][0]=path[v];
            v=path[v];
            if(v==-1)
            {
                cont--;
                break;
            }
        }
        int output=0;
        for(int i=0;i<cont;i++)
        {
            int xx=ans[i][0];
            int yy=ans[i][1];
            int tmp=map[xx][yy];
            map[xx][yy]=map[yy][xx]=0x3f3f3f3f;
            SPFA(1);
            if(dis[n]!=0x3f3f3f3f)
            output=max(output,dis[n]);
            map[xx][yy]=map[yy][xx]=tmp;
        }
        printf("%d\n",output);
    }
}

相对于边炒鸡多的时候,好像N^2突然占了便宜。

裸dijkstra+邻接矩阵实现 G++C++都可通过 2000+ms


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[1005][1005];
int path[1005];
int dis[1005];
int vis[1005];
int ans[100000][2];
int n,m,guanjian;
void init()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            map[i][j]=0x3f3f3f3f;
        }
    }
}
void Dij(int x)
{
    memset(vis,0,sizeof(vis));
    memset(path,-1,sizeof(path));
    for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
    dis[x]=0;
    for(int i=1;i<n;i++)
    {
        int k;
        int tmp=0x3f3f3f3f;
        for(int j=1;j<=n;j++)
        {
            if(dis[j]<tmp&&vis[j]==0)
            {
                tmp=dis[j];
                k=j;
            }
        }
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(vis[j]==0&&map[k][j]+dis[k]<dis[j])
            {
                dis[j]=dis[k]+map[k][j];
                path[j]=k;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            if(map[x][y]>w)
            {
                map[x][y]=map[y][x]=w;
            }
        }
        Dij(1);
        int cont=0;
        int v=n;
        while(1)
        {
            ans[cont][1]=v;
            ans[cont++][0]=path[v];
            v=path[v];
            if(v==-1)
            {
                cont--;
                break;
            }
        }
        int output=0;
        for(int i=0;i<cont;i++)
        {
            int xx=ans[i][0];
            int yy=ans[i][1];
            int tmp=map[xx][yy];
            map[xx][yy]=map[yy][xx]=0x3f3f3f3f;
            Dij(1);
            if(dis[n]!=0x3f3f3f3f)
            output=max(output,dis[n]);
            map[xx][yy]=map[yy][xx]=tmp;
        }
        printf("%d\n",output);
    }
}

队列优化(STL)+Dij+邻接矩阵,好像效果不是很好0.0

也是2000+ms


AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
    int u,len;
    friend bool operator<(node a,node b)
    {
        return a.len>b.len;
    }
}now,nex;
int map[1005][1005];
int path[1005];
int dis[1005];
int vis[1005];
int ans[100000][2];
int n,m,guanjian;
void init()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            map[i][j]=0x3f3f3f3f;
        }
    }
}
void priority_Dij(int x)
{
    priority_queue<node>s;
    memset(vis,0,sizeof(vis));
    memset(path,-1,sizeof(path));
    for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
    dis[x]=0;
    now.u=x;
    now.len=0;
    s.push(now);
    while(!s.empty())
    {
        now=s.top();
        s.pop();
        vis[now.u]=1;
        int u=now.u;
        for(int i=1;i<=n;i++)
        {
            int v=i;
            if(vis[v]==1)continue;
            if(dis[v]>dis[u]+map[u][v])
            {
                dis[v]=dis[u]+map[u][v];
                nex.u=v;
                nex.len=dis[v];
                s.push(nex);
                path[v]=u;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            if(map[x][y]>w)
            {
                map[x][y]=map[y][x]=w;
            }
        }
        priority_Dij(1);
        int cont=0;
        int v=n;
        while(1)
        {
            ans[cont][1]=v;
            ans[cont++][0]=path[v];
            v=path[v];
            if(v==-1)
            {
                cont--;
                break;
            }
        }
        int output=0;
        for(int i=0;i<cont;i++)
        {
            int xx=ans[i][0];
            int yy=ans[i][1];
            int tmp=map[xx][yy];
            map[xx][yy]=map[yy][xx]=0x3f3f3f3f;
            priority_Dij(1);
            if(dis[n]!=0x3f3f3f3f)
            output=max(output,dis[n]);
            map[xx][yy]=map[yy][xx]=tmp;
        }
        printf("%d\n",output);
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值