传送门

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. sstable, bigtable,leveldb,cassandra,hbase的lsm基础

    先看懂文献1和2 1. 先了解sstable.SSTable: Sorted String Table [2] [10] WiscKey:  类似myisam, key value分离, 根据ssd优 ...

  2. ios之UITabelViewCell的自定义(xib实现2)

    上篇文章介绍了如何用UITableView显示表格,并讲了几种UITableViewCell的风格.不过有时候我们需要自己定义 UITableViewCell的风格,其实就是向行中添加子视图.添加子视 ...

  3. 调用 C 动态库

    调用 C 动态库 由 王巍 (@ONEVCAT) 发布于 2015/11/04 C 是程序世界的宝库,在我们面向的设备系统中,也内置了大量的 C 动态库帮助我们完成各种任务.比如涉及到压缩的话我们很可 ...

  4. Python爬虫环境常用库安装

    1:urllib urllib.request这两个库是python自带的库,不需要重新安装,在python中输入如下代码: import urllibimport urllib.requestres ...

  5. GIMP暗黑诱惑,部分彩色效果制作

    在一些图形处理中经常会用到高逼格的部分彩色,其他部分黑白的效果,今天我就简单记录一下如何操作. 1.选区,先选择要突出的选区,可以用多种方法,钢笔,套绳,小剪刀等等: 2.把选择的区域稍稍调整亮一点: ...

  6. ASP.NET使用Memcached高缓存实例的初级介绍

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...

  7. (转)ios 代码规范

    转自http://blog.csdn.net/pjk1129/article/details/45146955 引子 在看下面之前,大家自我检测一下自己写的代码是否规范,代码风格是否过于迥异阅读困难? ...

  8. 在 shell中, 我們可用 $0, $1, $2, $3 ... 這樣的变量分別提取命令行中变量

    代码: script_name parameter1 parameter2 parameter3 ...我們很容易就能猜出 $0 就是代表 shell script 名称(路径)本身,而 $1 就是其 ...

  9. eclipse中新建maven项目无法添加src/main/java问题

    eclipse创建maevn web项目,在选择maven_archetype_web原型后,默认只有src/main/resources这个Source Floder. 按照maven目录结构,添加 ...

  10. PAT Basic 1064

    1064 朋友数 如果两个整数各位数字的和是一样的,则被称为是“朋友数”,而那个公共的和就是它们的“朋友证号”.例如 123 和 51 就是朋友数,因为 1+2+3 = 5+1 = 6,而 6 就是它 ...