传送门

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. Google Colab的一些注意事项

    1.执行命令行前面加! 当我们使用python解释器时,我们需要不停地在命令行和IDE 之间切换,当我们需要使用命令行工具时.不过,Jupyter Notebook给了我们在notebook中运行sh ...

  2. javascript“类”与继承总结和回顾

    Javascipt语法不支持"类"(class)[es6已经支持],但是有模拟类的方法.今天我主要谈谈Javascipt中模拟“类”的方法及js中继承的总结和回顾. js中实现“类 ...

  3. 题解 CF440A 【Forgotten Episode】

    博客阅读更好 虽然这道题是紫题,但实际难度应该是橙题吧 首先,看到标签…… 紫题?但题目也太…… 这道题教会我们不要看标签 好了,废话少说,看到楼下许多大佬都用了数组,但我觉得可以不用 为什么? 我也 ...

  4. fossil 代理设置

    C:\>fossil user new Joe C:\>fossil user default Joe 设置账户 fossil setting proxy http://-:-fossil ...

  5. Hibernate 多表查询 - Criteria添加子字段查询条件 - 出错问题解决

    Criteria 查询条件如果是子对象中的非主键字段会报 could not resolve property private Criteria getCriteria(Favorite favori ...

  6. Python IDE推荐

    八个最佳Python IDE 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Python是一种功能强大.语言简洁的编程语言.本文向大家推荐8个适合pyt ...

  7. lucene测试类

    package test.lucene; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStrea ...

  8. ios之UIActionSheet

    UIActionSheet是在IOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件. 为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view applicatio ...

  9. 【dp】拔河比赛

    01背包:感谢ZCK大佬 题目描述 学校举行拔河比赛,所有的人被分成了两组,每个人必须(且只能够)在其中的一组,要求两个组的人数相差不能超过1,且两个组内的所有人体重加起来尽可能地接近. 输入 输入中 ...

  10. Python解答力扣网站题库简单版----第三讲

    1041. 困于环中的机器人 题库链接: 1041. 困于环中的机器人. 题干 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G" ...