【JZOJ2702】【GDKOI2012模拟02.01】探险

17 篇文章 0 订阅

Description

这里写图片描述

Data Constraint

对于30%的数据,n<=500,m<=10000
对于60%的数据,n<=2000,m<=50000
对于100%的数据,n<=10000,m<=200000,1<=w,v<=10000

Solution

我们要求最后不能经过同一暗道两次,所以我们用spfa求出起点到每个点在第一条出边不相同的情况下的最短路和次短路。然后枚举一条直接连向起点的边,若该点的最短路就是这条边的话我们就用次短路+这条边更新答案,否则用最短路+这条边更新答案。因为最短路会保证同一条边不经过两次,所以路径不会重复。

Code

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=4e5+5;
int first[maxn],last[maxn],next[maxn],cost[maxn],v[maxn*10],p[maxn];
int d[maxn],bz[maxn],d1[maxn],b[maxn],b1[maxn],mi[maxn];
int n,m,i,t,j,k,l,x,y,z,ans,num,yy,zz;
void lian(int x,int y,int z){
    last[++num]=y;next[num]=first[x];first[x]=num;cost[num]=z;p[num]=i;
}
void spfa(){
    j=i=0;memset(d,127,sizeof(d));memset(d1,127,sizeof(d1));d[1]=0;bz[1]=1;
    for (t=first[1];t;t=next[t]){
        if (d[last[t]]>cost[t])d1[last[t]]=d[last[t]],b1[last[t]]=b[last[t]],d[last[t]]=cost[t],b[last[t]]=p[t];
        else if (d1[last[t]]>cost[t]) d1[last[t]]=cost[t],b1[last[t]]=p[t];
        v[++j]=last[t];
    }
    while (i<j){
        x=v[++i];
        for (t=first[x];t;t=next[t]){
            if (last[t]<2) continue;
            y=d[last[t]];z=d1[last[t]];yy=b[last[t]];zz=b1[last[t]];
            if (d[last[t]]>d[x]+cost[t]){
                if (b[last[t]]!=b[x]) d1[last[t]]=d[last[t]],b1[last[t]]=b[last[t]];
                d[last[t]]=d[x]+cost[t];b[last[t]]=b[x];
                if (d1[last[t]]>d1[x]+cost[t] && b1[x]!=b[last[t]]) d1[last[t]]=d1[x]+cost[t],b1[last[t]]=b1[x];
            }else if (d1[last[t]]>d[x]+cost[t] && b[x]!=b[last[t]]) d1[last[t]]=d[x]+cost[t],b1[last[t]]=b[x];
            if (!bz[last[t]] && (y!=d[last[t]]||z!=d1[last[t]] || yy!=b[last[t]] || zz!=b1[last[t]])) v[++j]=last[t],bz[v[j]]=1;
        }
        bz[x]=0;
    }
}
int main(){
//  freopen("data.in","r",stdin);
    scanf("%d%d",&n,&m);
    memset(mi,127,sizeof(mi));
    for (i=1;i<=m;i++)
        scanf("%d%d%d%d",&x,&y,&z,&k),lian(x,y,z),lian(y,x,k);
    spfa();ans=2e9+5;
    for (x=1;x<=n;x++)
        for (t=first[x];t;t=next[t]){
            if (last[t]!=1) continue;
            if (b[x]!=p[t]) ans=min(ans,d[x]+cost[t]);
            else ans=min(ans,d1[x]+cost[t]);
        }
    printf("%d\n",ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值