题目描述

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.

样例输入

  1. 8 11 1 0 5
  2. 0 1 10
  3. 0 2 10
  4. 1 2 10
  5. 2 6 40
  6. 6 7 10
  7. 5 6 10
  8. 3 5 15
  9. 3 6 40
  10. 3 4 20
  11. 1 4 20
  12. 1 3 20
  13. 4 7

样例输出

  1. 45
  2.  
  3. 题意:n个点,m条双向边,f条单向边,单向边只能用一条,且费用为0,从st的最短路。
    思路:分层建图,两个图之间用费用为0的单向边连接,跑dijstra
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const int maxn = ;
  6. const int ad=5e5+;
  7. typedef pair<ll,int>pli;
  8. struct Node
  9. {
  10. int y,val,next;
  11. Node(int y=,int val=,int next=):y(y),val(val),next(next) {}
  12. } node[maxn<<];
  13.  
  14. int head[ad<<];
  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=;
  33. node[cnt].next=head[x];
  34. head[x]=cnt;
  35. }
  36. priority_queue<pli,vector<pli>,greater<pli> >que;
  37. bool vis[ad<<];
  38. ll dist[ad<<];
  39. void dijstra()
  40. {
  41. while(!que.empty())
  42. que.pop();
  43. memset(dist,0x3f,sizeof(dist));
  44. memset(vis,,sizeof(vis));
  45. que.push(pli(,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]=;
  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 = ;
  70. memset(head,,sizeof(head));
  71. for(int i=; 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=; i<=f; i++)
  79. {
  80. int u,v;
  81. scanf("%d%d",&u,&v);
  82. add2(u,v);
  83. }
  84. dijstra();
  85. printf("%lld\n",min(dist[t],dist[ad+t]));
  86. }

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. I2C(二) linux2.6

    目录 I2C(二) linux2.6 总线驱动 关键结构 入口 i2c_add_adapter 硬件操作 设备驱动 入口 注册 attach_adapter eeprom_detect i2c_att ...

  2. 快速掌握Nginx(四) —— Nginx日志切片和常用配置总结

    1.Nginx日志管理 1.日志简单介绍 Nginx提供了日志记录的功能,日志文件在对我们管理网站十分有用,通过访问日志(access_log)我们可以获取请求来源.客户端信息.请求的资源等信息:通过 ...

  3. nginx配置vue项目部署访问无问题,刷新出现404问题

    现象: 在浏览器中直接访问www.test.com/api1/login会404.但如果你先访问www.test.com后再点“登录" 跳转到www.test.com/api1/login是 ...

  4. maven配置阿里镜像仓库

    打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在<mirrors></mirrors>标签中添加mirror子节点 ...

  5. CORS跨域 Ajax headers 问题

    今天我们遇到了一个CORS跨域的问题Ajax如下 var url = "http://localhost:11980/api/Demo/GetString"; //api地址 $. ...

  6. vue 点击展开显示更多 点击收起部分隐藏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. thinkphp在iis上不是出现500错误

    按照官方文档,部署好iis下面URL重定向文件后,出现500错误,不停地百度,不停地修改web.config文件,终也不成. 在虚拟空间调整了php版本,一下子就好了.原来的版本为5.4,调整为5.6 ...

  8. Shiro权限模型以及权限分配的两种方式

    1. 顶级账户分配权限用户需要被分配相应的权限才可访问相应的资源.权限是对于资源的操作一张许可证.给用户分配资源权限需要将权限的相关信息保存到数据库.这些相关内容包含:用户信息.权限管理.用户分配的权 ...

  9. TV 丽音(NICAM)功能

    丽音意谓「接近即时的缩扩音频多路广播」 丽音使用数码技术,把电视台发送的两条音频讯号数码化后进行压缩,传送后再在接收机里扩大还原.数码化使用的是14位元 PCM,32千赫采样. 这种广播法可以播出优质 ...

  10. MyOD(课下作业,选做)

    MyOD(课下作业,选做) 代码要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.b ...