传送门

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. 【转】数据库SQL的一些总结

    http://www.cnblogs.com/yank/category/104903.html

  2. Robotium实践之路基于APK创建测试项目

    1.重新对包进行签名操作 .启动re-sign.jar文件 .找到相应的APK,拖拽置resigner中 2.创建基于APK测试的测试工程 .新建一个安卓测试项目 .选择this project

  3. CPP-基础:关于引用

    1.什么是“引用”?申明和使用“引用”要注意哪些问题? 引用就是某个目标变量的“别名”(alias),对应用的操作与对变量直接操作效果完全相同. 申明一个引用的时候,切记要对其进行初始化. 引用声明完 ...

  4. DirectX9(翻译):介绍

    一.简介 二.DirectX Software Development Kit 这本帮助文档总共分为五大部分:DirectX Software Development Kit DirectX Grap ...

  5. 毛毛虫组【Beta】Scrum Meeting 3

    第三天 日期:2019/6/25 前言 第三次会议: 时间:6月25日 地点:教10-A511 内容:此次会议主要是对项目验收做准备工作. 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: ...

  6. Java中的线程--多线程面试题

    到这里,基本上线程的并发中的知识点都是学到了,到了最后,还有三道面试题,从面试题中学习更加的加深一下,多线程中的知识点,如何在实际的问题中来解决多线程的问题,可以更好的从实际出发 一.面试题1 面试题 ...

  7. bootstrap 翻页(对齐的链接)

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. bootstrap历练实例:标签式的导航菜单

    本章将讲解bootstrap提供的用于定义导航元素的一些选项,它使用相同的标签和基类.nav.Bootsrtap也提供了一个用于共享标记和状态的帮助器类.改变修饰的class,可以在不同的样式间进行切 ...

  9. 【树形dp】vijos1144小胖守皇宫

    细节很精妙 描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步 ...

  10. 文件操作-dd

    Linux dd命令 用于读取.转换并输出数据. dd可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件.设备或标准输出. 参数说明: if=文件名: 输入文件名,缺省为标准输入.即 ...