传送门

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. UVA 11400 Lighting System Design 照明系统设计

    首先是一个贪心,一种灯泡要么全都换,要么全都不换. 先排序,定义状态d[i]为前面i种灯泡的最小花费,状态转移就是从d[j],j<i,加上 i前面的j+1到i-1种灯泡换成i的花费. 下标排序玩 ...

  2. IOS Array 排序方法

    NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerVal ...

  3. winhex 中磁盘大小与偏移

    下图为c盘(活动分区).上方base offset为相对于整个硬盘的字节偏移量.partition 1中信息包括c盘开始扇区,总扇区数.partition 2 信息为扩展分区开始扇区和扇区数.由 P1 ...

  4. 【Qt】2.2 继续了解信号和槽

    槽和普通成员函数一样,可以是虚函数.被重载,可以是公有.私有.保护的.它可以被其它C++成员函数调用. 槽连接了信号,当发射这个信号时,槽会被自动调用. 连接函数: bool QObject::con ...

  5. Day5 集合的深浅copy

    集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. 关系测试, ...

  6. 洛谷 P1126 机器人搬重物 (BFS)

    题目链接:https://www.luogu.org/problemnew/show/P1126 吐槽:这题很阴险 一开始没把格子图转化成点图:30分 转化成点图,发现样例过不去,原来每步要判断vis ...

  7. 第五次作业:Excel制作英文课程表

    要求: 一.内外变宽线条与颜色图同,表格有底纹色彩 二.横向打印,上下左右居中,表格标题居中,表头斜线,斜线两边加文字 三.设置打开密码

  8. Django项目部署:使用uwsgi和nginx的方式

    一.背景 前两天制作的个人博客网站基本完工,大致功能具备.但是在部署环节却也处处碰壁,这里也来总结以下,以备将来不时查看以及完善. 二.前提 2.1 需要的知识 django Django是一个基于p ...

  9. mysql 备份解密脚本

    #!/bin/bash #by sk 备份解码脚本 echo "-------------------------------------------------" functio ...

  10. vim之替换命令

    格式:<range>s /<pat1>/<pat2>/gc <range>用来指定替换命令执行的范围: 百分号(%)表示所有行 点(.)表示当前行 美元 ...