题目描述

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 flight. 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
 #include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = ;
const int ad=5e5+;
typedef pair<ll,int>pli;
struct Node
{
int y,val,next;
Node(int y=,int val=,int next=):y(y),val(val),next(next) {}
} node[maxn<<]; int head[ad<<];
int cnt;
int n,m,f,s,t;
void add1(int x,int y,int val)
{
node[++cnt].y=y;
node[cnt].val=val;
node[cnt].next=head[x];
head[x]=cnt;
node[++cnt].y=y+ad;
node[cnt].val=val;
node[cnt].next=head[x+ad];
head[x+ad]=cnt;
} void add2(int x,int y)
{
node[++cnt].y=y+ad;
node[cnt].val=;
node[cnt].next=head[x];
head[x]=cnt;
}
priority_queue<pli,vector<pli>,greater<pli> >que;
bool vis[ad<<];
ll dist[ad<<];
void dijstra()
{
while(!que.empty())
que.pop();
memset(dist,0x3f,sizeof(dist));
memset(vis,,sizeof(vis));
que.push(pli(,s));
while(!que.empty())
{
pli tmp = que.top();
que.pop();
int k = tmp.second;
ll v = tmp.first;
if(vis[k])
continue;
vis[k]=;
dist[k]=v;
for(int i=head[k]; i; i=node[i].next)
{
int to=node[i].y;
if(dist[to] > v+node[i].val)
{
que.push(pli(v+node[i].val,to));
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&n,&m,&f,&s,&t);
cnt = ;
memset(head,,sizeof(head));
for(int i=; i<=m; i++)
{
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
add1(u,v,k);
add1(v,u,k);
}
for(int i=; i<=f; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add2(u,v);
}
dijstra();
printf("%lld\n",min(dist[t],dist[ad+t]));
}

Bumped! 2017 ICPC North American Qualifier Contest (分层建图+dijstra)的更多相关文章

  1. ICPC North Central NA Contest 2018

    目录 ICPC North Central NA Contest 2018 1. 题目分析 2. 题解 A.Pokegene B.Maximum Subarrays C.Rational Ratio ...

  2. 【BZOJ-1570】BlueMary的旅行 分层建图 + 最大流

    1570: [JSOI2008]Blue Mary的旅行 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 388  Solved: 212[Submit ...

  3. 2019 ACM/ICPC North America Qualifier G.Research Productivity Index(概率期望dp)

    https://open.kattis.com/problems/researchproductivityindex 这道题是考场上没写出来的一道题,今年看看感觉简单到不像话,当时自己对于dp没有什么 ...

  4. The North American Invitational Programming Contest 2017 题目

    NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K   A mysterious circular arrangement of black st ...

  5. The North American Invitational Programming Contest 2018 D. Missing Gnomes

    A family of nn gnomes likes to line up for a group picture. Each gnome can be uniquely identified by ...

  6. The North American Invitational Programming Contest 2018 H. Recovery

    Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4: \displaystyle \ ...

  7. The North American Invitational Programming Contest 2018 E. Prefix Free Code

    Consider nn initial strings of lower case letters, where no initial string is a prefix of any other ...

  8. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  9. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

随机推荐

  1. 帕斯卡(pascal)命名法:

    帕斯卡(pascal)命名法: 与骆驼命名法类似.只不过骆驼命名法是首字母小写,而帕斯卡命名法是首字母大写 如:public void DisplayInfo(); string UserName; ...

  2. DirectX11 With Windows SDK--23 立方体映射:动态天空盒的实现

    前言 上一章的静态天空盒已经可以满足绝大部分日常使用了.但对于自带反射/折射属性的物体来说,它需要依赖天空盒进行绘制,但静态天空盒并不会记录周边的物体,更不用说正在其周围运动的物体了.因此我们需要在运 ...

  3. echo 输入背景和字体常用方法

               ECHO输出背景颜色以及文字颜色输出格式:  echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"例如:        echo -e & ...

  4. [译]asp-net-core-mvc-ajax-form-requests-using-jquery-unobtrusive

    原文 全文源码 开始项目 项目使用了package.json'文件,添加需要的前端package到项目中.在这我们添加了jquery-ajax-unobstrusive`. { "versi ...

  5. python中opencv的安装

    1.得到opencv的安装包: 2.把安装包中的cv.py, cv2.pd放到一个文件夹中,并把这个文件夹放到D:\Anaconda2\Lib\site-packages中: 3.添加新的变量,变量名 ...

  6. HTTP常见的状态码——面试题常考

    一些常见的状态码为:  200 - 服务器成功返回网页 400(错误请求)服务器不理解请求的语法. 404 - 请求的网页不存在 500(服务器内部错误)服务器遇到错误,无法完成请求. 503 - 服 ...

  7. CodeChef - COUNTREL Count Relations

    题目链接 给你一个长为NNN的1,2,3,....N1,2,3,....N1,2,3,....N的序列,让你求出两种关系各个有多少可能: R1R_1R1​,由于x,yx,yx,y互不是子集,且交集为空 ...

  8. Collection和Map类图预览与比较

    类图 HashSet和TreeSet的区别:https://www.cnblogs.com/bobi1234/p/10759769.html HashSet和LinkedHashSet区别:https ...

  9. Django first lesson 环境搭建

    pycharm ide集成开发环境 (提高开发效率) 解释器/编译器 编辑器 调试环境 虚拟机连接 设置VirtualBox端口 操作1 操作2 点击+号添加,名称为SSH,其中主机端口为物理机的端口 ...

  10. (四)ORBSLAM运动估计

    ORBSLAM2的运动估计简介 ORBSLAM2中的运动估计核心方法就是3D-2D的PNP,而在跟踪过程主要分为三种类型: 无运动模型的跟踪,即基于参考帧的跟踪: 基于匀速运动模型的跟踪: 重定位: ...