题解:
        2维权重dij, 先距离最短, 后改变最小。
 
在这个题中, 如果要改变最小, 则让更多的可用边放进来。
然后可以用pre存下关键边。
 
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
vector<pll> vc[N];
struct Node{
int o, u, d, p;
bool operator < (const Node & x) const{
if(d == x.d) return p > x.p;
return d > x.d;
}
};
int pre[N];
int vis[N];
priority_queue<Node> pq;
void dij(){
pq.push({, , , });
while(!pq.empty()){
Node t = pq.top();
pq.pop();
if(vis[t.u]) continue;
vis[t.u] = ;
pre[t.u] = t.o;
for(pll tmp : vc[t.u]){
if(vis[tmp.fi]) continue;
pq.push({t.u, tmp.fi, t.d+, t.p + !tmp.se});
}
}
}
vector<Node> ans;
int fpre[N];
int main(){
int n, m, u, v, op;
scanf("%d%d", &n, &m);
for(int i = ; i <= m; ++i){
scanf("%d%d%d", &u, &v, &op);
vc[u].pb(pll(v, op));
vc[v].pb(pll(u, op));
}
dij();
int x = n;
while(x){
fpre[x] = pre[x];
x = pre[x];
}
for(int i = ; i <= n; ++i){
for(pll &t : vc[i]){
if(t.fi < i){
int v = t.fi;
if((fpre[i] == v || fpre[v] == i)){
if(!t.se)
ans.pb({v, i, , });
}
else if(t.se)
ans.pb({v, i, , });
}
}
}
printf("%d\n", ans.size());
for(Node & t : ans){
printf("%d %d %d\n", t.o, t.u, t.d);
}
return ;
}

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. 目标检测评价指标mAP 精准率和召回率

    首先明确几个概念,精确率,召回率,准确率 精确率precision 召回率recall 准确率accuracy 以一个实际例子入手,假设我们有100个肿瘤病人. 95个良性肿瘤病人,5个恶性肿瘤病人. ...

  2. containerd与kubernetes集成

    kubernetes集群三步安装 概念介绍 cri (Container runtime interface) cri is a containerd plugin implementation of ...

  3. Activiti6系列(4)- 三个war包的数据源及密码修改

    一.activiti-app修改数据源和密码 1.使用sublimetext工具打开tomcat,方便进行配置文件的修改. 找到被解压的war包,activiti-app/WEB-INF/classe ...

  4. A solution to the never shortened to-do list

    I once told my younger sister my learning system, and the basic five doctrines of my methodology. Bu ...

  5. 趣味CSS3效果挑战小汇总

    众所周知,在CSS3中产生了诸多优秀的特性,现在就来分享一下我这段时间对于这些特性的效果实践,希望对大家有所启发. 挑战1: 画一个对话框 要画一个对话框,首先来学习做一个三角形.其实非常的简单. & ...

  6. 安装VMware14虚拟机,centos7版本的linux 软件地址

    首先下载虚拟机软件和centos7的linux系统的镜像软件系统, https://pan.baidu.com/s/1cJfzpaLwB4dfe2W8gGEAPQ 两个文件 非常好用 虚拟机安装 很简 ...

  7. AutoCAD二次开发(.Net)之创建图层Layer

    //https://blog.csdn.net/qq_21489689?t=1[CommandMethod("CREATELY")] public void CreateLayer ...

  8. Zabbix监控华为路由器配置

    zabbix监控华为路由器 一.配置路由器界面: 开启snmpv2c功能,同时勾选v2c版本(不要勾v1,不安全,其实v2也不安全,如果很注重安全的话,一定要只配置v3). 手里没有路由器登录账号,大 ...

  9. 逻辑回归(Logistic Regression)详解,公式推导及代码实现

    逻辑回归(Logistic Regression) 什么是逻辑回归: 逻辑回归(Logistic Regression)是一种基于概率的模式识别算法,虽然名字中带"回归",但实际上 ...

  10. Springboot源码分析之代理三板斧

    摘要: 在Spring的版本变迁过程中,注解发生了很多的变化,然而代理的设计也发生了微妙的变化,从Spring1.x的ProxyFactoryBean的硬编码岛Spring2.x的Aspectj注解, ...