洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's ( spfa,最短路径) 题解

94 篇文章 0 订阅

题目来源:

https://www.luogu.org/problemnew/show/P3106

题目描述:

题目描述

Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice when selecting extra features for the car, and as a result the car ended up equipped with two GPS navigation systems! Even worse, the two systems often make conflicting decisions about the route that FJ should take.

The map of the region in which FJ lives consists of N intersections (2 <= N <= 10,000) and M directional roads (1 <= M <= 50,000). Road i connects intersections A_i (1 <= A_i <= N) and B_i (1 <= B_i <= N). Multiple roads could connect the same pair of intersections, and a bi-directional road (one permitting two-way travel) is represented by two separate directional roads in opposite orientations. FJ's house is located at intersection 1, and his farm is located at intersection N. It is possible to reach the farm from his house by traveling along a series of directional roads.

Both GPS units are using the same underlying map as described above; however, they have different notions for the travel time along each road. Road i takes P_i units of time to traverse according to the first GPS unit, and Q_i units of time to traverse according to the second unit (each travel time is an integer in the range 1..100,000).

FJ wants to travel from his house to the farm. However, each GPS unit complains loudly any time FJ follows a road (say, from intersection X to intersection Y) that the GPS unit believes not to be part of a shortest route from X to the farm (it is even possible that both GPS units can complain, if FJ takes a road that neither unit likes).

Please help FJ determine the minimum possible number of total complaints he can receive if he chooses his route appropriately. If both GPS units complain when FJ follows a road, this counts as +2 towards the total.

给你一个N个点的有向图,可能有重边.

有两个GPS定位系统,分别认为经过边i的时间为Pi,和Qi.

每走一条边的时候,如果一个系统认为走的这条边不是它认为的最短路,就会受到警告一次T T

两个系统是分开警告的,就是说当走的这条边都不在两个系统认为的最短路范围内,就会受到2次警告.

如果边(u,v)不在u到n的最短路径上,这条边就受到一次警告,求从1到n最少受到多少次警告。

输入输出格式

输入格式:

 

* Line 1: The integers N and M.

Line i describes road i with four integers: A_i B_i P_i Q_i.

 

输出格式:

 

* Line 1: The minimum total number of complaints FJ can receive if he routes himself from his house to the farm optimally.

 

输入输出样例

输入样例#1: 复制

5 7 
3 4 7 1 
1 3 2 20 
1 4 17 18 
4 5 25 3 
1 2 10 1 
3 5 4 14 
2 4 6 5 

输出样例#1: 复制

1 

说明

There are 5 intersections and 7 directional roads. The first road connects from intersection 3 to intersection 4; the first GPS thinks this road takes 7 units of time to traverse, and the second GPS thinks it takes 1 unit of time, etc.

If FJ follows the path 1 -> 2 -> 4 -> 5, then the first GPS complains on the 1 -> 2 road (it would prefer the 1 -> 3 road instead). However, for the rest of the route 2 -> 4 -> 5, both GPSs are happy, since this is a shortest route from 2 to 5 according to each GPS.

解题思路:

       这题还是很有收获,一共用了3次spfa,本题关键就是怎么在求出最短距离之后,判断某一条边是否在最短路上,这里用的是反向建边,求出n到所有点的最短距离,在判断u到v是否为最短路径是我们就是判断,dis【u】==dis【v】+e【i】.cost,相等就是,不等就不是,一开始建边边权为0,然后最后不是在其中一个系统的最短路上的边边权++,在跑一次spfa就行,代码交烦,重复的很多,。。。。

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int  maxn=1e5+10;
struct newt
{
    int to,next,cost;
}e1[maxn*5],e2[maxn*5],e3[maxn*5];
int head1[maxn],head2[maxn],head3[maxn],dis1[maxn],dis2[maxn],dis3[maxn],cnt1=0,cnt2=0,cnt3=0,n,m,vis[maxn];
void addedge1(int u,int v,int w)
{
    e1[cnt1].to=v;
    e1[cnt1].cost=w;
    e1[cnt1].next=head1[u];
    head1[u]=cnt1++;
}
void addedge2(int u,int v,int w)
{
    e2[cnt2].to=v;
    e2[cnt2].cost=w;
    e2[cnt2].next=head2[u];
    head2[u]=cnt2++;
}
void addedge3(int u,int v,int w)
{
    e3[cnt3].to=v;
    e3[cnt3].cost=w;
    e3[cnt3].next=head3[u];
    head3[u]=cnt3++;
}
void spfa1()
{
    for(int i=1;i<=n;i++)dis1[i]=inf,vis[i]=0;
    queue<int>q;
    q.push(n);
    vis[n]=1;
    dis1[n]=0;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head1[now];i!=-1;i=e1[i].next)
        {
            int v=e1[i].to;
            if(dis1[v]>dis1[now]+e1[i].cost)
            {
                dis1[v]=dis1[now]+e1[i].cost;
                if(vis[v])continue;
                q.push(v);
                vis[v]=1;
            }
        }
    }
}
void spfa2()
{
    for(int i=1;i<=n;i++)dis2[i]=inf,vis[i]=0;
    queue<int>q;
    q.push(n);
    vis[n]=1;
    dis2[n]=0;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head2[now];i!=-1;i=e2[i].next)
        {
            int v=e2[i].to;
            if(dis2[v]>dis2[now]+e2[i].cost)
            {
                dis2[v]=dis2[now]+e2[i].cost;
                if(vis[v])continue;
                q.push(v);
                vis[v]=1;
            }
        }
    }
}
void spfa3()
{
    for(int i=1;i<=n;i++)dis3[i]=inf,vis[i]=0;
    queue<int>q;
    q.push(1);
    vis[1]=1;
    dis3[1]=0;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head3[now];i!=-1;i=e3[i].next)
        {
            int v=e3[i].to;
            if(dis3[v]>dis3[now]+e3[i].cost)
            {
                dis3[v]=dis3[now]+e3[i].cost;
                if(vis[v])continue;
                q.push(v);
                vis[v]=1;
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)head1[i]=-1,head2[i]=-1,head3[i]=-1;
    for(int i=1;i<=m;i++)
    {
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        addedge1(b,a,c);
        addedge2(b,a,d);
        addedge3(a,b,0);
    //	printf("%d %d\n",a,b);
    }
    spfa1(),spfa2();
    for(int u=1;u<=n;u++)
    {
        for(int i=head3[u];i!=-1;i=e3[i].next)
        {
            int v=e3[i].to;
            
            if(dis1[u]!=dis1[v]+e1[i].cost)
            e3[i].cost++;
            if(dis2[u]!=dis2[v]+e2[i].cost)
            e3[i].cost++;
            //printf("%d %d %d %d %d\n",u,v,dis1[v],dis1[u],e1[i].cost);
        }
    }
    spfa3();
    printf("%d\n",dis3[n]);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值