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

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
这道题我觉得是很简单的,因为思路很容易就想出来了,首先我们需要找出一条最短路,并且我们需要路径,就是边序列,然后枚举这跳路径的边,即假设没有这条边,之所要在这枚举,是因为假设非这条路径的其他边不存在,那么当前的就是一条可行路的最短路了。后来我一直来想实现边的记录,最后就是,唯一id标志,也就是说每条边都有一个id,最后找出的的一条路径就是一条id,不过真的自己的代码能力还不行,写的很快,debug花了好长时间。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#define INF 0x7fffffff
#define N 500005
using namespace std;
struct node
{
    int x1,x2;
    int w;
}edge[N];
vector<int> g[1005];
int n,m;
int path[1005];
int dis[1005];
bool inQ[1005];
int now[1005];//找出一条路径的id
int k;
int index;
queue<int> que;
void  spfa()
{
    for(int i=2;i<=n;i++)
        dis[i]=INF;
    dis[1]=0;
    inQ[1]=true;
    que.push(1);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        inQ[u]=false;
        for(int i=0;i<g[u].size();i++)
        {
            index=g[u][i];
            int v=(u==edge[index].x1?edge[index].x2:edge[index].x1);//应该肯定有一点与之相同,我们要找不同的点
            //cout<<"haha";
            int w=edge[index].w;
            if(dis[v]>dis[u]+w)
            {
                path[v]=index;//path说明是那一条边转移来的
                dis[v]=dis[u]+w;
                if(!inQ[v])
                {
                    que.push(v);
                    inQ[v]=true;
                }
            }
        }
    }
    int temp=n;
    while(dis[temp])//从n往回找就找到了一条路径id
    {
        //cout<<dis[temp]<<endl;
        now[k++]=path[temp];
        temp=edge[path[temp]].x1==temp?edge[path[temp]].x2:edge[path[temp]].x1;
    }

}
void init(int x)
{
    for(int i=1;i<=n;i++)
    g[i].clear();
    k=0;
}
int main()
{
    int x,y,w;
    while(scanf("%d%d",&n,&m)==2)
    {
        int ans=-1;
        init(n);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&w);
            edge[i].x1=x,edge[i].x2=y,edge[i].w=w;//边的标识就是数组下标
            g[x].push_back(i);
            g[y].push_back(i);//然后存下标
        }

        spfa();
        //cout<<"k:"<<k<<endl;
        for(int j=0;j<k;j++)//枚举id即可,在做spfa
        {
            for(int i=2;i<=n;i++)
                dis[i]=INF;
            dis[1]=0;
            inQ[1]=true;
            que.push(1);
            while(!que.empty())
            {
                int u=que.front();
                que.pop();
                inQ[u]=false;
                for(int i=0;i<g[u].size();i++)
                {
                    index=g[u][i];
                    if(index==now[j])
                        continue;
                    int v=(u==edge[index].x1?edge[index].x2:edge[index].x1);
                    //cout<<"haha";
                    int w=edge[index].w;
                    if(dis[v]>dis[u]+w)
                    {
                        dis[v]=dis[u]+w;
                        if(!inQ[v])
                        {
                            que.push(v);
                            inQ[v]=true;
                        }
                    }
                }
            }
            //cout<<dis[n]<<endl;
            ans=max(ans,dis[n]);        
        }
        printf("%d\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值