Bumped! 2017 ICPC North American Qualifier Contest (分层建图+dijstra)

Posted 进击的黑仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bumped! 2017 ICPC North American Qualifier Contest (分层建图+dijstra)相关的知识,希望对你有一定的参考价值。

题目描述

Peter returned from the recently held ACM ICPC World finals only to find that his return flight was overbooked and he was bumped from the flight! Well, at least he wasn’t beat up by the
airline and he’s received a voucher for one free flight between any two destinations he wishes.
He is already planning next year’s trip. He plans to travel by car where necessary, but he may be using his free flight ticket for one leg of the trip. He asked for your help in his planning.
He can provide you a network of cities connected by roads, the amount it costs to buy gas for traveling between pairs of cities, and a list of available flights between some of those cities. Help Peter by finding the minimum amount of money he needs to spend to get from his hometown to next year’s destination!

 

输入

The input consists of a single test case. The first line lists five space-separated integers n, m, f, s, and t, denoting the number of cities n (0 < n ≤ 50 000), the number of roads m (0 ≤ m ≤ 150 000), the number of flights f (0 ≤ f ≤ 1 000), the number s (0 ≤ s < n) of the city in which Peter’s trip starts, and the number t (0 ≤ t < n) of the city Peter is trying to travel to. (Cities are numbered from 0 to n − 1.)
The first line is followed by m lines, each describing one road. A road description contains three space-separated integers i, j, and c (0 ≤ i, j < n, i 6= j and 0 < c ≤ 50 000), indicating there is a road connecting cities i and j that costs c cents to travel. Roads can be used in either direction for the same cost. All road descriptions are unique.
Each of the following f lines contains a description of an available flight, which consists of two space-separated integers u and v (0 ≤ u, v < n, u 6= v) denoting that a flight from city u to city v is available (though not from v to u unless listed elsewhere). All flight descriptions are unique.

 

输出

Output the minimum number of cents Peter needs to spend to get from his home town to the competition,using at most one ?ight. You may assume that there is a route on which Peter can reach his destination.

 

样例输入

8 11 1 0 5
0 1 10
0 2 10
1 2 10
2 6 40
6 7 10
5 6 10
3 5 15
3 6 40
3 4 20
1 4 20
1 3 20
4 7

样例输出

45

题意:n个点,m条双向边,f条单向边,单向边只能用一条,且费用为0,从s到t的最短路。
思路:分层建图,两个图之间用费用为0的单向边连接,跑dijstra

技术图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long ll;
 5 const int maxn = 301005;
 6 const int ad=5e5+4;
 7 typedef pair<ll,int>pli;
 8 struct Node
 9 {
10     int y,val,next;
11     Node(int y=0,int val=0,int next=0):y(y),val(val),next(next) {}
12 } node[maxn<<1];
13 
14 int head[ad<<1];
15 int cnt;
16 int n,m,f,s,t;
17 void add1(int x,int y,int val)
18 {
19     node[++cnt].y=y;
20     node[cnt].val=val;
21     node[cnt].next=head[x];
22     head[x]=cnt;
23     node[++cnt].y=y+ad;
24     node[cnt].val=val;
25     node[cnt].next=head[x+ad];
26     head[x+ad]=cnt;
27 }
28 
29 void add2(int x,int y)
30 {
31     node[++cnt].y=y+ad;
32     node[cnt].val=0;
33     node[cnt].next=head[x];
34     head[x]=cnt;
35 }
36 priority_queue<pli,vector<pli>,greater<pli> >que;
37 bool vis[ad<<1];
38 ll dist[ad<<1];
39 void dijstra()
40 {
41     while(!que.empty())
42         que.pop();
43     memset(dist,0x3f,sizeof(dist));
44     memset(vis,0,sizeof(vis));
45     que.push(pli(0,s));
46     while(!que.empty())
47     {
48         pli tmp = que.top();
49         que.pop();
50         int k = tmp.second;
51         ll v = tmp.first;
52         if(vis[k])
53             continue;
54         vis[k]=1;
55         dist[k]=v;
56         for(int i=head[k]; i; i=node[i].next)
57         {
58             int to=node[i].y;
59             if(dist[to] > v+node[i].val)
60             {
61                 que.push(pli(v+node[i].val,to));
62             }
63         }
64     }
65 }
66 int main()
67 {
68     scanf("%d%d%d%d%d",&n,&m,&f,&s,&t);
69     cnt = 0;
70     memset(head,0,sizeof(head));
71     for(int i=1; i<=m; i++)
72     {
73         int u,v,k;
74         scanf("%d%d%d",&u,&v,&k);
75         add1(u,v,k);
76         add1(v,u,k);
77     }
78     for(int i=1; i<=f; i++)
79     {
80         int u,v;
81         scanf("%d%d",&u,&v);
82         add2(u,v);
83     }
84     dijstra();
85     printf("%lld
",min(dist[t],dist[ad+t]));
86 }
View Code

 

以上是关于Bumped! 2017 ICPC North American Qualifier Contest (分层建图+dijstra)的主要内容,如果未能解决你的问题,请参考以下文章

ICPC North Central NA Contest 2017 部分题解

2019-2020 ICPC North-Western Russia Regional Contest

ICPC 2019-2020 North-Western Russia Regional Contest E. Equidistant(分层)

ICPC 2019-2020 North-Western Russia Regional Contest E. Equidistant(分层)

05.24 ICPC 2019-2020 North-Western Russia Regional Contest复现赛+Codeforces Round #645 (Div. 2)

05.24 ICPC 2019-2020 North-Western Russia Regional Contest复现赛+Codeforces Round #645 (Div. 2)