【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- 背包dp+打表处理——cf999F
考虑每种c都是可以独立进行计算的,所以这题的答案等价于每种c的最优解之和 计算每种c的最优解:把问题转化成求出每种c的最大值,再转化成i个人分j张卡片的最大收益 dp[i,j]表示i个人分j张卡片的最 ...
- HashMap(常用)方法个人理解
Hashmap的存值: public static void main(String[] args) { ///*Integer*/map.put("1", 1);//向map ...
- EL fmt标签
c:formate 表达式需要传入的对象为date
- 没有找到mfc100.dll
转自VC错误:http://www.vcerror.com/?p=86 问题描述: 生成的exe文件在编译的时候会提示"没有找到mfc100.dll",这个时候需要更改配置为静态编 ...
- Keras+Yolo 目标检测
参考:https://www.cnblogs.com/tensorflownews/p/8922359.html Github:https://github.com/qqwweee/keras-yol ...
- iOS开发NSMutableArray数组越界处理
#import "NSArray+CrashArray.h" #import <objc/runtime.h> @implementation NSObject (Un ...
- 最全的机器学习&深度学习入门视频课程集
资源介绍 链接:http://pan.baidu.com/s/1kV6nWJP 密码:ryfd 链接:http://pan.baidu.com/s/1dEZWlP3 密码:y82m 更多资源 ...
- POJ-1260-Pearls-dp+理解题意
In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jew ...
- js 实现加载百分比效果
效果: html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- netty 解决粘包拆包问题
netty server TimeServer package com.zhaowb.netty.ch4_3; import io.netty.bootstrap.ServerBootstrap; i ...