Time limit2000 ms

Memory limit262144 kB

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 ncities with m bidirectional 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?

Input

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 integers x, y, z (1 ≤ x, y ≤ n) meaning that there is a road connecting cities number x and y. If z = 1, this road is working, otherwise it is not.

Output

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.

Examples

Input
2 1
1 2 0
Output
1
1 2 1
Input
4 4
1 2 1
1 3 0
2 3 1
3 4 1
Output
3
1 2 0
1 3 1
2 3 0
Input
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
Output
3
2 3 0
1 5 0
6 8 1

Note

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

题目意思:就是给你一个n个点和m条边,每条边有编号z=1边是好的,z=0边是坏的,问从点1出发到点n的最短路,当最短路有多条时取修正值小的结果。修正的值=多余的好路(边)+要用的坏路(边),输入输出有格式。

分析:最短路+记录前驱

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=;
int n,m,sum;
int book[maxn];
struct node
{
int c;
int v;
};
struct edge
{
int a,b,c;
}e[maxn];
vector<node>v[maxn];
queue<int>q;
int n0[maxn];
int n1[maxn];
int t[maxn];
int pra[maxn];
int main()
{
scanf("%d %d",&n,&m);
sum=;
for(int i=;i<=n;i++)
{
pra[i]=i;
n0[i]=;
n1[i]=;
book[i]=;
}
memset(t,inf,sizeof(t));
for(int i=;i<=m;i++)
{
int x,y,c;
scanf("%d %d %d",&x,&y,&c);
if(c==) sum++;//记录1的个数
e[i].a=x;
e[i].b=y;
e[i].c=c;
node a;
a.v=y;
a.c=c;
v[x].push_back(a);
a.v=x;
a.c=c;
v[y].push_back(a);
}
while(!q.empty()) q.pop();
q.push();
t[]=;
n0[]=;//这条路上有几个0
n1[]=;//有几个1
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=;i<v[x].size();i++)
{
node a=v[x][i];
int y=a.v;
if(t[y]>t[x]+)//用来记录路径的长短
{//选路径短的
t[y]=t[x]+;
if(a.c==) n0[y]=n0[x]+;
else n0[y]=n0[x];
if(a.c==) n1[y]=n1[x]+;
else n1[y]=n1[x];
pra[y]=x;//记录路径,不能写成pra【x】=y
q.push(y);
}
else if(t[y]==t[x]+)
{//如果路径相同
if(n0[y]>n0[x])
{//选0的个数少的,因为0少,修的路少,路径相同的情况下,0少1就多
if(a.c==) n0[y]=n0[x]+;
else n0[y]=n0[x];
if(a.c==) n1[y]=n1[x]+;
else n1[y]=n1[x];
pra[y]=x;
}
q.push(y);
}
} }
//cout<<n0[n]<<" "<<n1[n]<<endl;
//cout<<sum<<endl;
printf("%d",n0[n]+sum-n1[n]);//输出的是0的个数,总共1的个数-当前路的1的个数
int k=n;
//cout<<pra[1]<<" "<<pra[4]<<" "<<pra[6]<<endl;
while(pra[k]!=k)//整理一下
{
book[k]=;
k=pra[k];
}
for(int i=;i<=n;i++)
{
if(!book[i])
{
pra[i]=i;//该恢复的恢复
}
}
for(int i=;i<=m;i++)
{
edge x=e[i];
if(pra[x.a]!=x.b&&pra[x.b]!=x.a&&x.c==)
{
printf("\n");
printf("%d %d %d",x.a,x.b,); }
else if(pra[x.a]==x.b||pra[x.b]==x.a)
{
if(x.c==)
{
printf("\n");
printf("%d %d %d",x.a,x.b,);
}
}
}
return ;
}

Breaking Good的更多相关文章

  1. How can I protect derived classes from breaking when I change the internal parts of the base class?

    How can I protect derived classes from breaking when I change the internal parts of the base class? ...

  2. SpriteBuilder实际操作中如何确定合适Breaking force的值

    确定Breaking force合适的值同样很单调,但是按照下面的方法也并不是完全不可能: 输入一个随意的值,比如说100 检查实际场景中关节是否能承受住物理物体,在完美的情况下物理物体将保持静止. ...

  3. SpriteBuilder中关节的Breaking force属性

    在SpriteBuilder中三种物理关节都包含Breaking force区域在属性框中. 该属性被设置成关节可以承受的压力临界值.如果关节的压力超出了Breaking force中设置的值,则关节 ...

  4. ElasticSearch 2 (3) - Breaking Changes

    ElasticSearch 2.1.1 (3) - Breaking Changes Search Changes search_type = scan Deprecated GET /my_ind ...

  5. 绝命毒师第五季/全集Breaking Bad迅雷下载

    本季Breaking Bad Season 5(2012)看点:故事紧接着上一季,通过一场精心策划的大爆炸,沃尔特(布莱恩·科兰斯顿 Bryan Cranston 饰)终于除掉了长久以来的威胁古斯塔沃 ...

  6. 绝命毒师第一季/全集Breaking Bad迅雷下载

    本季Breaking Bad Season 1(2008)看点:新墨西哥州的高中化学老师沃尔特·H·怀特(布莱恩·科兰斯顿 Bryan Cranston 饰)是拮据家庭的唯一经济来源.他大半生安分守己 ...

  7. Codeforces Round #287 (Div. 2) E. Breaking Good 最短路

    题目链接: http://codeforces.com/problemset/problem/507/E E. Breaking Good time limit per test2 secondsme ...

  8. Known BREAKING CHANGES from NH3.3.3.GA to 4.0.0

    Build 4.0.0.Alpha1 =============================   ** Known BREAKING CHANGES from NH3.3.3.GA to 4.0. ...

  9. 【转载】【翻译】Breaking things is easy///机器学习中安全与隐私问题(对抗性攻击)

    原文:Breaking things is easy 译文:机器学习中安全与隐私问题(对抗性攻击) 我是通过Infaraway的那篇博文才发现cleverhans-blog的博客的,这是一个很有意思的 ...

随机推荐

  1. javaMail发送邮件实例

    背景:最近项目里有个实时发送邮件的功能,今天闲下来整理 一下,记录下来方便以后直接使用. 代码: package com.dzf.utils; import java.io.File; import ...

  2. CentOS/Linux 卸载MATLAB

    rm -rf /usr/local/MATLAB/R2012arm /usr/local/bin/matlab /usr/local/bin/mcc /usr/local/bin/mex /usr/l ...

  3. YII2笔记之三

    activeform布局findAll等不能满足where, order by, limit,底层调用了findByConditioncol-md col-lg的使用 view的方法,前三个常用ren ...

  4. ubuntu/centos printk 终端中不能打印信息及解决办法

    今天用ubuntu来调试信息,printk死活打印不出信息,即使把级别跳到<0>,即KERN_ALERT也不行,后再搜了好长时间网络, 这个地址:http://bbs.chinaunix. ...

  5. 用TinyXml做XML解析示例 TinyXml查找唯一节点及修改节点操作

    // 读者对象:对TinyXml有一定了解的人.本文是对TinyXml工具的一些知识点的理解. // 1 TinyXml中对TiXmlNode进行了分类,是用一个枚举进行描述的. // enum No ...

  6. Guest CPU model configuration in libvirt with QEMU/KVM

    每个hypervisor对于guest能看到的cpu model定义都不同,Xen 提供host pass through,所以guest能看到的cpu和host完全相同. QEMU/KVM中gues ...

  7. Ngnix学习笔记

    一.Ngnix介绍 1.概念 一个强大的Web服务器软件. 2.功能 1)处理高并发的http请求. 2)作为反向代理服务器来进行负载均衡. 3)数据压缩和解压缩处理 3.优势 高性能,轻量级,内存消 ...

  8. eclipse web项目导入itellij idea并启动

    概述 主要分为项目配置和tomcat配置两大步骤. 一.项目配置 打开idea,选择导入项 选择将要打开的项目路径后,继续选择项目的原本类型(后续引导设置会根据原本的项目类型更新成idea的项目),此 ...

  9. 考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传

    最近做的一个需求,要通过PHP调用cURL,以multipart/form-data格式上传文件.踩坑若干,够一篇文章了. 重要警告 没事不要读PHP的官方中文文档!版本跟不上坑死你! 不同版本PHP ...

  10. NYOJ-626-intersection set(二分查找)

    题目链接 /* Name:NYOJ-626-intersection set Copyright: Author: Date: 2018/4/12 21:30:10 Description: 二分查找 ...