CodeForces P507E Breaking Good

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


寬搜好題目,道路長度固定爲1,所以很容易想到用寬搜。多條到n的最短路如何取捨呢,如果先一路寬搜到底,統計所有最短路信息的話,估計會被有些數據卡掉,其實不用這麼做。其實只要選那條最短路並且這條路上影響最少的就行了。你可以自己證明一下,很容易證明。所以寬搜在優先保證最短的同時,考慮之前影響最少。這道題還叫我們輸出全部被影響的道路。這只是個小技巧,你只要把選擇的路記下來就行了,所以一開始記得對道路標記index,並且這index在寬搜記錄道路中也要記錄進去。


代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<vector>
#include<cmath>
#include<map>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mod 1000000007
using namespace std;
struct edge{
  int e,op,ind;	
}eg[200020],pre[100020];
struct node{
  int s,e,v;	
}res[100020];
int n,m,m2,n2,dp[100020],fir[100020],nxt[200020],lst[100020]; 
int dis[100020];
bool d[100020];
queue<int> qu;
void addedge(int s,int e,int v,int ind){
  m2++; eg[m2].e=e; eg[m2].op=v; eg[m2].ind=ind;
  if(fir[s]==-1){
    fir[s]=m2; lst[s]=m2;	
  }	
  else{
    nxt[lst[s]]=m2; lst[s]=m2;	
  }
}
int main()
{
	int i,j;
	
	while(scanf("%d%d",&n,&m)!=EOF){
	  MM(dp,0); MM(dis,-1); MM(fir,-1); MM(d,0); MM(nxt,-1);
	  rep(i,m){
  	    int s,e,v;
		scanf("%d%d%d",&s,&e,&v);
		res[i].s=s; res[i].e=e; res[i].v=v;
		addedge(s,e,v,i); addedge(e,s,v,i);	
  	  }
  	  n2=0;
  	  while(!qu.empty()) qu.pop();
  	  qu.push(1); dis[1]=0;
  	  while(!qu.empty()){
  	    int s=qu.front(),e,op,ind; qu.pop();
		for(i=fir[s];i!=-1;i=nxt[i]){
		  e=eg[i].e; op=eg[i].op; ind=eg[i].ind;
		  if(dis[e]==-1){
  		    dis[e]=dis[s]+1;
  		    dp[e]=dp[s]+1-op;
			qu.push(e);	
			pre[e].e=s; pre[e].op=op; pre[e].ind=ind;
  		  }	
  		  else if(dis[s]+1==dis[e] && dp[s]+1-op<dp[e]){
  		         dp[e]=dp[s]+1-op;	
  		         pre[e].e=s; pre[e].op=op; pre[e].ind=ind;
  		       }	
		}	
  	  }
  	  i=n;
  	  while(1){
  	    int s=pre[i].e,e=i,op=pre[i].op,ind=pre[i].ind;
		d[ind]=1; i=s;
		if(s==1) break;
  	  }
  	  rep(i,m){
  	    if(d[i] && res[i].v!=1) n2++;
		if(!d[i] && res[i].v!=0) n2++;	
  	  } 
  	  printf("%d\n",n2);
  	  rep(i,m){
  	    if(d[i] && res[i].v!=1) printf("%d %d %d\n",res[i].s,res[i].e,1);
		if(!d[i] && res[i].v!=0) printf("%d %d %d\n",res[i].s,res[i].e,0);	
  	  } 
	}

	
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章