【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- scala中Array简单实用
/** * 在scala中数组的使用 * 和java很类似,初始化后,长度就固定了,而且元素全部根据其类型初始化 * */ object arrayUse { def main(args: Array ...
- 10 行 Python 代码实现模糊查询/智能提示
10 行 Python 代码实现模糊查询/智能提示 1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的 ...
- SSL和TLS漏洞验证
工具下载:git clone https://github.com/drwetter/testssl.sh.git 实验环境:192.168.1.22(bee-box v1.6) 192.168.1. ...
- <随便写> 多线程的例子
''' 一个线程在使用这个共享的时候,其他线程必须等待他结束 通过"锁"实现,作用就是防止多个线程使用这片内存空间 进程:程序的一次执行 线程:cpu运算的基本调度单位 多线程:大 ...
- WebClient 上传文件 上传文件到服务器端
一直对于上传文件到服务器端困惑:以前,现在,学到了关于WebClient的post知识 瞬间对于上传文件到服务器觉得好轻松: 原理很简单:我们通过post服务器的页面:把本地的文件直接传递过去: 现在 ...
- iOS开发系列-NSURLConnection
概述 NSURLConnection是负责发送请求,建立客户端与服务端的连接.发送数据给服务器,并收集来自服务器的响应数据.其中NSURLRequest类是用来封装一个请求,包含NSURL对象.请求方 ...
- spring:AOP面向切面编程(注解)03
使用注解写aop时最好使用环绕通知写 切面类: /** * 用于记录日志的工具类,它里面提供了公共的代码 */ @Component("logger") @Aspect //表示当 ...
- T2960 全民健身【思维Dp,预处理,差分优化】
Online Judge:YCJSOI Label:Dp,思维题,预处理,滚动优化 题目描述 乐乐现在掌管一个大公司,办公楼共有n层.为了增加员工的身体素质,他决定在每层楼都建立一个活动室,活动室分乒 ...
- DOM节点的创建、插入、删除
值得注意的是:节点的创建.插入以及删除都是操作父级容器.(1)创建var newDiv = documnet.createElement('div'); ——创建的元素只能操作一次 (2)插入/追加a ...
- leetcode-106-从中序和后序遍历构造二叉树
题目描述: 方法一:O(n) O(n) class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -& ...