I - Harry Potter and the Final Battle HDU - 3986 最短路加重边,dijkstra的邻接表实现

本文介绍了一种基于哈利波特与伏地魔对决背景的最短路径算法问题,通过邻接表存储图并利用Dijkstra算法求解在允许破坏一条边情况下的最差情形最短路径。
The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case. 
Input
First line, case number t (t<=20). 
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities. 
Output
Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”. 
Sample Input
3
4
4
1 2 5
2 4 10
1 3 3
3 4 8
3
2
1 2 5
2 3 10
2
2
1 2 1
1 2 2
Sample Output
15
-1
2   
n个点,分别标为1-n,n个点之间有m条路,现在可以毁坏m条路中的任意一条路,让其无法通行,现在让你求最差的情况下的最短路径长度;
分析:刚开始以为先将最短路径的长度求出来,并保存最短路的路径,然后再遍历最短路的每一条边,将其长度改为INF,如此可求得答案,可最后发现这个图是有重边的,只能使用邻接边来存储,并且还要处理好重边 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1010
#define M 50050
#define INF 1<<28
struct Edge
{
    int v,next,w;///next可以保存连在一起的边在edge中的位置
} edge[M*2];///由于是无向边,所以乘以二
int n,m,cnt;
int head[N];///保存节点N在edge中的id
int vis[N],d[N];
struct Node
{
    int pre,id;
} used[N];///used[i].pre存储点i的上一个节点,used[i].id存储点i在edge中的地址(都是最短路上的信息)
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void djstl(int s,int t)///第一次是1和0
{
    for(int i=1; i<=n; i++)
    {
        d[i]=INF;
        if(!t) used[i].pre=s;///s为起点
    }
    for(int u=head[s]; u!=-1; u=edge[u].next)
    {
        if(edge[u].w<d[edge[u].v])
        {
            d[edge[u].v]=edge[u].w;
            if(!t) used[edge[u].v].id=u;
        }
    }
    memset(vis,0,sizeof(vis));
    vis[s]=1;
    for(int i=1; i<=n; i++)
    {
        int maxn=INF;
        int v=-1;
        for(int j=1; j<=n; j++)
            if(!vis[j]&&maxn>d[j])
            {
                maxn=d[j];
                v=j;
            }
        if(v==-1) continue;
        vis[v]=1;
        for(int u=head[v]; u!=-1; u=edge[u].next)
        {
            int e=edge[u].v;
            if(!vis[e]&&d[v]+edge[u].w<d[e])
            {
                d[e]=d[v]+edge[u].w;
                if(!t) used[e].pre=v,used[e].id=u;
            }
        }
    }
}
int main()
{
    int s,t,w;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        init();
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&s,&t,&w);
            addedge(s,t,w);
            addedge(t,s,w);
        }
        djstl(1,0);
        int maxn=d[n];
        for(int i=n; i!=1; i=used[i].pre)
        {
            int val=edge[used[i].id].w;///used[i].id在这里的作用非常大,他将提供了最短路在edge中的id,让其能够将其赋值INF
            edge[used[i].id].w=INF;
            djstl(1,1);
            edge[used[i].id].w=val;
            maxn=max(maxn,d[n]);
        }
        if(maxn>=INF) printf("-1\n");
        else printf("%d\n",maxn);
    }
    return 0;
}
用邻接表来存储有重边的图,并用了一个used[i].id来记录最短路中的节点i在edge中的id,以便方便讲
该条边的长度赋值为无限大
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值