USACO2014Open Silver GPS的决斗

Problem 2: Dueling GPS’s [Brian Dean, 2014]

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.

可以先反着Dijkstra求出每一个点到终点的距离,然后在任何一个点都可以知道对于它的最短路径是多长;
所以可以很容易知道有没有违反(只需要把对于这两个点的两条最短路的差和它们之间的边权比较一下就好了)
然后从一个点到另一个点的违反次数都可求了.
最后在以违反次数为边权值正着Dijkstra一遍就好了.

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define pb push_back
const int M=10005;
struct node {
    int x,val;
    bool operator < (const node &t) const {
        return val>t.val;
    }
};
#define meg(x,y) (node){x,y}

vector <int> val[5][M];
vector <int> e[2][M];   //有些数组其实不用,但是开了也没什么关系

int dis[3][M];
void BFS(int f,int k,int id,int st) {
    bool mark[M]={0};
    priority_queue <node> bfs;
    bfs.push(meg(st,0));
    while (!bfs.empty()) {
        node now=bfs.top();
        bfs.pop();
        int x=now.x,v=now.val;
        if (mark[x]) continue;
        dis[id][x]=v;
        mark[x]=1;
        for (int i=0;i<e[f][x].size();++i) {
            int to=e[f][x][i];
            int vl=val[k][x][i];
            bfs.push(meg(to,v+vl));
        }
    }
}

void init(int n) {
    for (int i=1;i<=n;++i) {
        for (int j=0;j<e[0][i].size();++j) {
            int x=i,to=e[0][i][j];
            int v1=val[0][i][j];
            int v2=val[1][i][j];
            int ans=0;
            if (dis[0][x]!=dis[0][to]+v1) ans++;
            if (dis[1][x]!=dis[1][to]+v2) ans++;
            val[4][i].pb(ans);
        }
    }
}

int main() {
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=m;++i) {
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        e[0][a].pb(b);
        e[1][b].pb(a);
        val[0][a].pb(c);
        val[1][a].pb(d);
        val[2][b].pb(c);
        val[3][b].pb(d);
    }
    BFS(1,2,0,n);
    BFS(1,3,1,n);
    init(n);
    BFS(0,4,2,1);
    cout<<dis[2][n]<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值