传送门

E. Breaking Good
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 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.

Sample test(s)
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条边。让你求出来1到n的最短路,但是要求,保证最短路的时候,要求权值最大。

最开始没反应过来是求最短路问题,直接bfs,,,跪惨。。。。

一开始我试图在结构体里面用个string类型的变量来记录路径,但是wa了,不懂为什么。
现在好像懂了,,,编号太大,,,爆char了,,,,果然string记录路径就是个坑,,,
 
9557892 2015-01-26 11:37:48 njczy2010 E - Breaking Good GNU C++ Accepted 124 ms 19100 KB
9557655 2015-01-26 11:12:44 njczy2010 E - Breaking Good GNU C++ Accepted 623 ms 19300 KB
9557440 2015-01-26 10:51:36 njczy2010 E - Breaking Good GNU C++ Wrong answer on test 8 31 ms 6100 KB
9557233 2015-01-26 10:29:53 njczy2010 E - Breaking Good GNU C++ Wrong answer on test 8 15 ms 6200 KB
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 100005
#define M 105
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m;
int x[N],y[N],z[N];
int tot; typedef struct
{
int a,b;
int f;
}AA; AA ans[N]; typedef struct
{
int to;
int f;
int indexb;
}PP; vector<PP>bian[N];
int vis[N];
int Step[N];
int Cnt[N]; struct QQ
{
friend bool operator < (QQ n1,QQ n2)
{
if(n1.step==n2.step){
return n1.cnt>n2.cnt;
}
else{
return n1.step>n2.step;
}
}
int index;
int cnt;
int indexn;
int pre;
int step;
}; void out(QQ te); void ini()
{
int i;
tot=;
memset(vis,,sizeof(vis));
PP te;
for(i=;i<=n;i++){
bian[i].clear();
Step[i]=inf;
Cnt[i]=inf;
}
Step[]=;
Cnt[]=;
for(i=;i<=m;i++){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
te.to=y[i];te.f=z[i];te.indexb=i;
bian[ x[i] ].push_back(te);
te.to=x[i];
bian[ y[i] ].push_back(te);
}
} typedef struct
{
int in;
int pre;
}NN; NN node[N*];
int totnode; void bfs()
{
priority_queue <QQ>q;
QQ te,nt;
te.index=;
te.cnt=;
te.step=;
totnode=;
te.indexn=-;
te.pre=-;
q.push(te);
int st;
PP yy;
vector<PP>::iterator it;
while(q.size()>=)
{
te=q.top();
q.pop();
if(te.index==n){
out(te);
return;
}
if(vis[te.index]==) continue;
vis[te.index]=;
st=te.index;
for(it=bian[st].begin();it!=bian[st].end();it++)
{
yy=*it;
if(vis[yy.to]==) continue;
nt.index=yy.to;
nt.cnt=te.cnt;
nt.pre=te.index;
nt.step=te.step+;
if(yy.f==){
nt.cnt++;
}
if(nt.step>Step[nt.index]) continue;
if(nt.step==Step[nt.index] && nt.cnt>Cnt[nt.index]) continue;
Step[nt.index]=nt.step;
Cnt[nt.index]=nt.cnt;
nt.indexn=totnode;
node[totnode].in=yy.indexb;
node[totnode].pre=te.indexn;
totnode++;
q.push(nt);
}
}
} void solve()
{
bfs();
} void out(QQ te)
{
int i;
int vis2[N];
memset(vis2,,sizeof(vis2));
i=te.indexn;
int temp;
while(i!=-)
{
temp=node[i].in;
vis2[temp]=;
i=node[i].pre;
}
for(i=;i<=m;i++){
if(vis2[i]==){
if(z[i]==){
ans[tot].a=x[i];
ans[tot].b=y[i];
ans[tot].f=;
tot++;
}
}
else{
if(z[i]==){
ans[tot].a=x[i];
ans[tot].b=y[i];
ans[tot].f=;
tot++;
}
}
}
printf("%d\n",tot);
for(i=;i<tot;i++){
printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].f);
}
} int main()
{
//freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
// out();
}
return ;
}

一开始wa在test8的代码:

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 100005
#define M 105
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m;
int x[N],y[N],z[N];
int tot; typedef struct
{
int a,b;
int f;
}AA; AA ans[N]; typedef struct
{
int to;
int f;
int indexb;
}PP; vector<PP>bian[N];
int vis[N];
int Step[N];
int Cnt[N]; struct QQ
{
friend bool operator < (QQ n1,QQ n2)
{
if(n1.step==n2.step){
return n1.cnt>n2.cnt;
}
else{
return n1.step>n2.step;
}
}
int index;
int cnt;
string s;
int indexn;
int pre;
int step;
}; void out(QQ te); void ini()
{
int i;
tot=;
memset(vis,,sizeof(vis));
PP te;
for(i=;i<=n;i++){
bian[i].clear();
Step[i]=inf;
Cnt[i]=inf;
}
Step[]=;
Cnt[]=;
for(i=;i<=m;i++){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
te.to=y[i];te.f=z[i];te.indexb=i;
bian[ x[i] ].push_back(te);
te.to=x[i];
bian[ y[i] ].push_back(te);
}
} typedef struct
{
int in;
int pre;
}NN; NN node[N*];
int totnode; void bfs()
{
priority_queue <QQ>q;
QQ te,nt;
te.index=;
te.cnt=;
te.step=;
te.s="";
totnode=;
te.indexn=-;
te.pre=-;
//vis[1]=1;
q.push(te);
int st;
PP yy;
char ss;
vector<PP>::iterator it;
while(q.size()>=)
{
te=q.top();
// printf(" index=%d cnt=%d",te.index,te.cnt);
// cout<<" s="<<te.s<<endl;
q.pop();
if(te.index==n){
out(te);
return;
}
if(vis[te.index]==) continue;
vis[te.index]=;
st=te.index;
for(it=bian[st].begin();it!=bian[st].end();it++)
{
yy=*it;
if(vis[yy.to]==) continue;
nt.index=yy.to;
ss=yy.indexb+'';
nt.s=te.s+ss;
// vis[yy.indexb]=1;
//vis[nt.index]=1;
nt.cnt=te.cnt;
nt.pre=te.index;
nt.step=te.step+;
if(yy.f==){
nt.cnt++;
}
if(nt.step>Step[nt.index]) continue;
if(nt.step==Step[nt.index] && nt.cnt>Cnt[nt.index]) continue;
Step[nt.index]=nt.step;
Cnt[nt.index]=nt.cnt;
nt.indexn=totnode;
node[totnode].in=yy.indexb;
node[totnode].pre=te.indexn;
totnode++;
q.push(nt); // printf(" index=%d cnt=%d",nt.index,nt.cnt);
// cout<<" s="<<nt.s<<endl;
}
}
} void solve()
{
bfs();
} void out(QQ te)
{
int l=te.s.length();
int i;
int vis2[N];
memset(vis2,,sizeof(vis2));
// cout<<"te.s="<<te.s<<" l="<<l<<endl;
i=te.indexn;
int temp;
while(i!=-)
{
temp=node[i].in;
vis2[temp]=;
i=node[i].pre;
}
// for(i=0;i<l;i++){
// vis2[ te.s[i]-'0' ]=1;
// }
for(i=;i<=m;i++){
if(vis2[i]==){
if(z[i]==){
ans[tot].a=x[i];
ans[tot].b=y[i];
ans[tot].f=;
tot++;
}
}
else{
if(z[i]==){
ans[tot].a=x[i];
ans[tot].b=y[i];
ans[tot].f=;
tot++;
}
}
}
printf("%d\n",tot);
for(i=;i<tot;i++){
printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].f);
}
} int main()
{
//freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
// out();
}
return ;
}

Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]的更多相关文章

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

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

  2. Codeforces Round #287 (Div. 2) E. Breaking Good 路径记录!!!+最短路+堆优化

    E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. 贪心 Codeforces Round #287 (Div. 2) A. Amr and Music

    题目传送门 /* 贪心水题 */ #include <cstdio> #include <algorithm> #include <iostream> #inclu ...

  4. 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 ...

  5. CodeForces Round #287 Div.2

    A. Amr and Music (贪心) 水题,没能秒切,略尴尬. #include <cstdio> #include <algorithm> using namespac ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. C# Process.Start方法

    System.Diagnostics.Process.Start(); 主要功能: 1.打开某个链接网址(弹窗). 2.定位打开某个文件目录. 3.打开系统特殊文件夹,如“控制面板”等. Proces ...

  2. Asp.Net Core 进阶(一) —— 读取appsettings.json

    我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...

  3. c++ 中的函数调用中的参数传递

    概述 初学 \(c++\),一直搞不懂其参数传递方式.故找到一篇不错的文章:刘志华的深入探讨C++语言中参数传递问题.亲自实践一遍,并作此记录,以加深印象.     主要内容 本文主要分为五个小部分, ...

  4. QT +自定义控件-spin+slider

    动手实现自定义控件: 1.首先在ui界面中添加一个(Widget)容器类.如图中的1所示 2.在项目中添加一个SmallWidget类,如下: 3.接着在程序编辑界面进行程序编辑如下: #includ ...

  5. PWN题搭建

    0x00.准备题目 例如:level.c #include <stdio.h> #include <unistd.h> int main(){ char buffer[0x10 ...

  6. (18)zabbix值映射Value mapping

    1. 介绍 zabbix为了显示更人性化的数据,在使用过程中,我们可以将获取到得数据映射为一个字符串. 比如,我们写脚本监控MySQL是否在运行中, 一般返回0表示数据库挂了,1表示数据库正常,还有各 ...

  7. MySql存储过程的调试

    写和调试存储过程比较好的工具是dbForge studio for mysql 校验其中临时表字段是否符合要求,在存储过程中动态为临时表添加字段约束,或者写个游标,把数据迭代出来,一个个判断.当游标迭 ...

  8. LeetCode(109) Convert Sorted List to Binary Search Tree

    题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...

  9. vscode python code-runner 中文乱码解决

    https://www.cnblogs.com/zhaoshizi/p/9050768.html 这里我使用的是第二种方法

  10. Chrome 开发者工具(DevTools)中所有快捷方式列表(已整理)

    Chrome 开发者工具(DevTools)中所有快捷方式列表(已整理) 前言 Chrome DevTools提供了一些内置的快捷键,开发者利用这些快捷键可以节省常工作中很多日的开发时间.下面列出了每 ...