Codeforces Round #287 (Div. 2) E. Breaking Good 路径记录!!!+最短路+堆优化
2 seconds
256 megabytes
standard input
standard output
Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers.
Walter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of n cities with mbidirectional roads connecting them. There is no road is connecting a city to itself and for any two cities there is at most one road between them. The country is connected, in the other words, it is possible to reach any city from any other city using the given roads.
The roads aren't all working. There are some roads which need some more work to be performed to be completely functioning.
The gang is going to rob a bank! The bank is located in city 1. As usual, the hardest part is to escape to their headquarters where the police can't get them. The gang's headquarters is in city n. To gain the gang's trust, Walter is in charge of this operation, so he came up with a smart plan.
First of all the path which they are going to use on their way back from city 1 to their headquarters n must be as short as possible, since it is important to finish operation as fast as possible.
Then, gang has to blow up all other roads in country that don't lay on this path, in order to prevent any police reinforcements. In case of non-working road, they don't have to blow up it as it is already malfunctional.
If the chosen path has some roads that doesn't work they'll have to repair those roads before the operation.
Walter discovered that there was a lot of paths that satisfied the condition of being shortest possible so he decided to choose among them a path that minimizes the total number of affected roads (both roads that have to be blown up and roads to be repaired).
Can you help Walter complete his task and gain the gang's trust?
The first line of input contains two integers n, m (2 ≤ n ≤ 105,
), the number of cities and number of roads respectively.
In following m lines there are descriptions of roads. Each description consists of three integersx, y, z (1 ≤ x, y ≤ n,
) meaning that there is a road connecting cities number xand y. If z = 1, this road is working, otherwise it is not.
In the first line output one integer k, the minimum possible number of roads affected by gang.
In the following k lines output three integers describing roads that should be affected. Each line should contain three integers x, y, z (1 ≤ x, y ≤ n,
), cities connected by a road and the new state of a road. z = 1 indicates that the road between cities x and y should be repaired and z = 0 means that road should be blown up.
You may output roads in any order. Each affected road should appear exactly once. You may output cities connected by a single road in any order. If you output a road, it's original state should be different from z.
After performing all operations accroding to your plan, there should remain working only roads lying on some certain shortest past between city 1 and n.
If there are multiple optimal answers output any.
2 1
1 2 0
1
1 2 1
4 4
1 2 1
1 3 0
2 3 1
3 4 1
3
1 2 0
1 3 1
2 3 0
8 9
1 2 0
8 3 0
2 3 1
1 4 1
8 7 0
1 5 1
4 6 1
5 7 0
6 8 0
3
2 3 0
1 5 0
6 8 1
In the first test the only path is 1 - 2
In the second test the only shortest path is 1 - 3 - 4
In the third test there are multiple shortest paths but the optimal is 1 - 4 - 6 - 8
给一个图G<V,E>,有N个点和M条边。G的边有两种属性:work or notwork.
现要求选择一条路径Path,满足以下条件:
1. Path起点是1,终点是N
2. Path是所有路经中最短的一条
3. Path里所有notwork的边都要被修复,Path外所有work的边都要被炸毁,且所修复和炸毁的边的总和数最小
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100000;
int vis[maxn+10],dis[maxn+10],pre[maxn+10],flag[maxn+10],num[maxn+10];
struct Edge{
int to,c,flag,u,v;
}e[maxn+10]; struct node{
int v,dis;
bool operator<(const node a) const{
return this->dis>a.dis;
}
}; vector<Edge> G[maxn+10]; void init(int n)
{
for(int i=1;i<=n;i++) G[i].clear();
MM(vis,0);
MM(dis,inf);
MM(pre,0);
MM(flag,0);
MM(num,0);
} int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
init(n);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].flag);
G[e[i].u].push_back((Edge){e[i].v,1,e[i].flag,e[i].u,e[i].v});
G[e[i].v].push_back((Edge){e[i].u,1,e[i].flag,e[i].u,e[i].v});
} priority_queue<node> q;
q.push((node){1,0});
vis[1]=1;
dis[1]=0;
while(q.size())
{
node cur=q.top();q.pop();
int u=cur.v;
vis[u]=1;
if(dis[u]<cur.dis) continue;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].to;
if(vis[v]) continue;
if(dis[v]>dis[u]+1)
{
dis[v]=dis[u]+1;
q.push((node){v,dis[v]});
pre[v]=u;
num[v]=num[u]+G[u][i].flag;
}//优先满足最短路
else if(dis[v]==dis[u]+1&&num[v]<num[u]+G[u][i].flag)
{
pre[v]=u;
num[v]=num[u]+G[u][i].flag;
}
}
} int cnt=0;
for(int i=n;i>=1;i=pre[i])
flag[i]=1; for(int i=1;i<=m;i++)
if(flag[e[i].u]&&flag[e[i].v])
{if(!e[i].flag) cnt++;}
else if(e[i].flag) cnt++; printf("%d\n",cnt);
for(int i=1;i<=m;i++)
if(flag[e[i].u]&&flag[e[i].v])
{if(!e[i].flag) printf("%d %d 1\n",e[i].u,e[i].v);}
else if(e[i].flag) printf("%d %d 0\n",e[i].u,e[i].v); }
return 0;
}
分析:这道题主要挂在怎么记录最短路上;
处理方法是给每个节点设置一个pre值,指向在符合要求的最短路上当前节点指向的上一节点,最后
用个flag数组保存路上节点就好,num[i]表示从起点到达i点的最短路上(因为需要先满足最短路)最大
的权值和
Codeforces Round #287 (Div. 2) E. Breaking Good 路径记录!!!+最短路+堆优化的更多相关文章
- Codeforces Round #287 (Div. 2) E. Breaking Good 最短路
题目链接: http://codeforces.com/problemset/problem/507/E E. Breaking Good time limit per test2 secondsme ...
- Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]
传送门 E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 贪心 Codeforces Round #287 (Div. 2) A. Amr and Music
题目传送门 /* 贪心水题 */ #include <cstdio> #include <algorithm> #include <iostream> #inclu ...
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 思路
C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CodeForces Round #287 Div.2
A. Amr and Music (贪心) 水题,没能秒切,略尴尬. #include <cstdio> #include <algorithm> using namespac ...
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 水题
C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #287 (Div. 2) B. Amr and Pins 水题
B. Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #287 (Div. 2) A. Amr and Music 水题
A. Amr and Music time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #287 (Div. 2) D. The Maths Lecture [数位dp]
传送门 D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input stan ...
随机推荐
- DataGridView中EnditCommit()调用之后,单元格的内容被全选了,每次输入都要鼠标点击定位到最后才能继续输入
因为某些需求,DataGridView在输入一次内容,就要调用ECommitEdit(DataGridViewDataErrorContexts.Commit)来将内容提交,但是这样做之后,控件就会当 ...
- for循环练习题:拆解字符并输入下标
test = input('请输入:') for item in range(0,len(test)): print(item,test[item])
- Editor placeholder in source code错误
When you insert code via autocompletion (or via a code snippet, sometimes), there may be placeholder ...
- Sqlserver限制用户访问指定数据库
USE master CREATE LOGIN test --要创建的用户名 WITH PASSWORD = '123456', --密码 DEFAULT_DATABASE = DBTest, --指 ...
- spring boot1.1 idea + springboot + mybatis(mybatis-generator) +mysql +html实现简单的登录注册
前言 这两年springboot比较火,而我平时的工作中不怎么使用spring boot,所以工作之余就自己写写项目练练手,也跟大家一起学习. 打算从最开始的搭架子,登录注册,到后台管理的增删改查,业 ...
- [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化
这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...
- 22、nlpir 人工智能
练习介绍 [程序功能] 我们将完成一个和语义识别相关的爬虫程序,输入任意词汇.句子.文章或段落,会返回联想的词汇. [背景信息] 有一个非常牛的处理语言的网站nlpir,上面有非常多的处理语言的功能( ...
- 在docker容器下利用数据卷实现在删除了mysql容器或者镜像的情况下恢复数据
当把mysql容器销毁,在新建一个容器,进行之前的数据恢复. 因为之前建立了数据卷,那么现在就可以利用这个数据卷进行数据恢复. 使用docker volume create volume_name命令 ...
- Vue进行路由跳转的几种方式
1.<router-link to="需要跳转到页面的路径"> 2.this.$router.push()跳转到指定的url,并在history中添加记录,点击回退返回 ...
- ES的索引、type、document、filer、mapping、id
一.ES的存储结构 1.索引 es 中存储数据的基本单位,比如说你现在要在 es 中存储一些订单数据,你就应该在 es 中创建一个索引 order_idx,所有的订单数据就都写到这个索引里面去.看了一 ...