题解:
        2维权重dij, 先距离最短, 后改变最小。
 
在这个题中, 如果要改变最小, 则让更多的可用边放进来。
然后可以用pre存下关键边。
 
代码:
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4. #define LL long long
  5. #define ULL unsigned LL
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. #define lson l,m,rt<<1
  10. #define rson m+1,r,rt<<1|1
  11. #define lch(x) tr[x].son[0]
  12. #define rch(x) tr[x].son[1]
  13. #define max3(a,b,c) max(a,max(b,c))
  14. #define min3(a,b,c) min(a,min(b,c))
  15. typedef pair<int,int> pll;
  16. const int inf = 0x3f3f3f3f;
  17. const int _inf = 0xc0c0c0c0;
  18. const LL INF = 0x3f3f3f3f3f3f3f3f;
  19. const LL _INF = 0xc0c0c0c0c0c0c0c0;
  20. const LL mod = (int)1e9+;
  21. const int N = 1e5 + ;
  22. vector<pll> vc[N];
  23. struct Node{
  24. int o, u, d, p;
  25. bool operator < (const Node & x) const{
  26. if(d == x.d) return p > x.p;
  27. return d > x.d;
  28. }
  29. };
  30. int pre[N];
  31. int vis[N];
  32. priority_queue<Node> pq;
  33. void dij(){
  34. pq.push({, , , });
  35. while(!pq.empty()){
  36. Node t = pq.top();
  37. pq.pop();
  38. if(vis[t.u]) continue;
  39. vis[t.u] = ;
  40. pre[t.u] = t.o;
  41. for(pll tmp : vc[t.u]){
  42. if(vis[tmp.fi]) continue;
  43. pq.push({t.u, tmp.fi, t.d+, t.p + !tmp.se});
  44. }
  45. }
  46. }
  47. vector<Node> ans;
  48. int fpre[N];
  49. int main(){
  50. int n, m, u, v, op;
  51. scanf("%d%d", &n, &m);
  52. for(int i = ; i <= m; ++i){
  53. scanf("%d%d%d", &u, &v, &op);
  54. vc[u].pb(pll(v, op));
  55. vc[v].pb(pll(u, op));
  56. }
  57. dij();
  58. int x = n;
  59. while(x){
  60. fpre[x] = pre[x];
  61. x = pre[x];
  62. }
  63. for(int i = ; i <= n; ++i){
  64. for(pll &t : vc[i]){
  65. if(t.fi < i){
  66. int v = t.fi;
  67. if((fpre[i] == v || fpre[v] == i)){
  68. if(!t.se)
  69. ans.pb({v, i, , });
  70. }
  71. else if(t.se)
  72. ans.pb({v, i, , });
  73. }
  74. }
  75. }
  76. printf("%d\n", ans.size());
  77. for(Node & t : ans){
  78. printf("%d %d %d\n", t.o, t.u, t.d);
  79. }
  80. return ;
  81. }

CodeForces 507E Breaking Good 2维权重dij的更多相关文章

  1. [Codeforces 507E] Breaking Good

    [题目链接] https://codeforces.com/contest/507/problem/E [算法] 首先BFS求出1到其余点的最短路 , N到其余点的最短路,记为distA[]和dist ...

  2. 【codeforces 507E】Breaking Good

    [题目链接]:https://vjudge.net/contest/164884#problem/D [题意] 给你一张图; 图中有些路是完好的;但有些路还没修好; 先不管路有没有修好; 问你从起点到 ...

  3. Codeforces 707 E. Garlands (二维树状数组)

    题目链接:http://codeforces.com/problemset/problem/707/E 给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值. 有q个操作,操作ask问 ...

  4. Codeforces 835C - Star sky - [二维前缀和]

    题目链接:http://codeforces.com/problemset/problem/835/C 题意: 在天空上划定一个直角坐标系,有 $n$ 颗星星,每颗星星都有坐标 $(x_i,y_i)$ ...

  5. Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分

    Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...

  6. codeforces 14E. Camels(多维dp)

    题目链接:http://codeforces.com/problemset/problem/14/E 题意:就是给出n个点要求画出t个波峰和t-1个波谷 很显然要t个波峰和t-1个波谷开始是波动一定是 ...

  7. B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路

    B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...

  8. Codeforces 450D Jzzhu and Cities [heap优化dij]

    #include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...

  9. Codeforces 838A - Binary Blocks(二维前缀和+容斥)

    838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...

随机推荐

  1. 【iOS】UIImageView 点击事件

    UIImageView 并不像 UIButton 那样点击鼠标就可以关联点击事件,也不像 Android 里有 onClickListener,这个时候就需要借助 UITapGestureRecogn ...

  2. postman使用pre-request script计算md5

    接口加了验签逻辑,具体是md5(salt+时间戳).被某君吐槽说测试不方便啊能不能先关掉.其实没有必要打开又关闭验签功能,postman的pre-request script功能完全可以模拟客户端加密 ...

  3. 注解与AOP切面编程实现redis缓存与数据库查询的解耦

    一般缓存与数据库的配合使用是这样的. 1.查询缓存中是否有数据. 2.缓存中无数据,查询数据库. 3.把数据库数据插入到缓存中. 其实我们发现 1,3 都是固定的套路,只有2 是真正的业务代码.我们可 ...

  4. 数据结构-二叉搜索树和二叉树排序算法(python实现)

    今天我们要介绍的是一种特殊的二叉树--二叉搜索树,同时我们也会讲到一种排序算法--二叉树排序算法.这两者之间有什么联系呢,我们一起来看一下吧. 开始之前呢,我们先来介绍一下如何创建一颗二叉搜索树. 假 ...

  5. [TCP/IP]DNS解析

    DNS解析主机的IP地址 host -t A www.baidu.com

  6. cinder支持nfs快照

    [问题描述] cinder后端设置为NFS,磁盘创建快照失败. 日志里面发现了这个错误: VolumeDriverException: Volume driver reported an error: ...

  7. 微信公众号发送消息给用户 php

    1.微信公众号 这里得话 一开始先去看了 微信公众号的接口 发现网页授权需要时认证的服务号,一开始想的是那去申请一个认证的服务号岂不是很费事,然后网上搜了搜,发现了还有微信公众号个人测试号这个东西,所 ...

  8. android ——后台下载

    这次的这个demo想要实现一个后台下载文件的功能,下载的时候会有一个告知进度的通知, 使用的依赖库就一个: compile 'com.squareup.okhttp3:okhttp:3.9.0' 大体 ...

  9. 【Python3爬虫】学习分布式爬虫第一步--Redis分布式爬虫初体验

    一.写在前面 之前写的爬虫都是单机爬虫,还没有尝试过分布式爬虫,这次就是一个分布式爬虫的初体验.所谓分布式爬虫,就是要用多台电脑同时爬取数据,相比于单机爬虫,分布式爬虫的爬取速度更快,也能更好地应对I ...

  10. socket基于TCP(粘包现象和处理)

    目录 6socket套接字 7基于TCP协议的socket简单的网络通信 AF_UNIX AF_INET(应用最广泛的一个) 报错类型 单一 链接+循环通信 远程命令 9.tcp 实例:远程执行命令 ...