【codeforces 507E】Breaking Good
【题目链接】:https://vjudge.net/contest/164884#problem/D
【题意】
给你一张图;
图中有些路是完好的;但有些路还没修好;
先不管路有没有修好;
问你从起点到终点的最短路;
如果最短路上有没修好的路,那么你要把它修好;
而不在最短路上的,如果是完好的路,你需要把它摧毁(???)
让你选出这么一条最短路,使得受影响的路(被摧毁的和修好的路的数目总和)最少;
【题解】
受影响的路即是:
最短路上的没修好的路+非最短路上的修好的路;
也即;
最短路的长度-最短路上的修好的路+整张图上修好的路-最短路上修好的路
也即
最短路的长度+整张图上修好的路-2*最短路上修好的路;
要让受影响的路最少;
即让最短路上修好的路最大就好;
这个可以在做spfa的时候顺便得到;
即最短路相同,再判断一下修好的路的个数;
按照那个公式算出受影响的路;
然后记录每个点的前缀;
最短路上没修好的路把它修好(输出);
不是最短路上的修好的路把它破坏(输出);
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+50;
const int INF = 0x3f3f3f3f;
struct abc{
int x,y,z;
};
int n,m,all1,k;
abc a[N];
vector <pii> G[N];
pii dis[N],pre[N];
queue <int> dl;
bool inq[N],bo[N];
void spfa()
{
rep1(i,1,n)
dis[i] = mp(INF,0);
dis[1].fi = 0;
dl.push(1);
inq[1] = true;
while (!dl.empty())
{
int x = dl.front();
dl.pop();
inq[x] = false;
for (pii temp:G[x])
{
int y = temp.fi,w = a[temp.se].z;
if (dis[y].fi>dis[x].fi+1 || (dis[y].fi==dis[x].fi+1 && dis[y].se<dis[x].se+w))
{
dis[y].fi = dis[x].fi+1;
dis[y].se = dis[x].se+w;
pre[y] = mp(x,temp.se);
if (!inq[y])
{
inq[y] = true;
dl.push(y);
}
}
}
}
}
int main()
{
//Open();
cin >> n >> m;
rep1(i,1,m)
{
cin >> a[i].x >> a[i].y >> a[i].z;
G[a[i].x].pb(mp(a[i].y,i));
G[a[i].y].pb(mp(a[i].x,i));
all1+=a[i].z;
}
spfa();
k = dis[n].fi+all1-2*dis[n].se;
cout << k << endl;
int now = n;
while (now!=1)
{
int temp = pre[now].se;
if (a[temp].z==0)
cout << a[temp].x <<' '<<a[temp].y<<' '<<1<<endl;
bo[temp] = 1;
now = pre[now].fi;
}
rep1(i,1,m)
if (!bo[i] && a[i].z==1)
cout << a[i].x <<' '<<a[i].y<<' '<<0<<endl;
return 0;
}
【codeforces 507E】Breaking Good的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- 怎样使用github?(转)
怎样使用github?(转) 转自: 怎样使用 GitHub? - 知乎https://www.zhihu.com/question/20070065 珊姗是个小太阳 ❤努力做最胖//bang的健身博 ...
- c#上传下载ftp(支持断点续传)
using System;using System.Net;using System.IO;using System.Text;using System.Net.Sockets;namespace f ...
- Ubunto 无法连接ssh客服端
解决办法: (1)查看ip地址是否冲突 我在单位的虚拟机ip地址是192.168.14.85,与其它机器冲突了.改成了192.168.14.83 (2)关闭Ubuntu14.04的防火墙 root ...
- 小tips: zoom和transform:scale的区别
小tips: zoom和transform:scale的区别 转自 张鑫旭 前端大神 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://w ...
- 1.1python基础_基础
1_编码 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: UTF-8 -*- ...
- 2019-11-7-C#-dotnet-线程不安全的弱引用缓存
title author date CreateTime categories C# dotnet 线程不安全的弱引用缓存 lindexi 2019-11-7 9:45:5 +0800 2019-11 ...
- Android开发 内存泄露检测框架LeakCanary
前言 挖坑后续填坑 中文网站:https://www.liaohuqiu.net/cn/posts/leak-canary-read-me/ gitbub:https://github.com/squ ...
- leetcode-95-不同的二叉搜索树②*
题目描述: 方法一:递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self. ...
- 前端CSS样式操作
目录 字体和文字 设置标签的宽高 字体属性 文字的属性 文字对齐 text-align 文字装饰 text-decoration 首行缩进 text-indent 背景属性 背景图片 边框 画圆 di ...
- sql.xml where ids in的写法
<if test="iSurfaceTypeArray != null"> AND b.i_SurfaceType in <!-- 根据外观检查查询 --> ...