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?
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.
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.
2 1 1 2 0
1 1 2 1
4 4 1 2 1 1 3 0 2 3 1 3 4 1
3 1 2 0 1 3 1 2 3 0
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
3 2 3 0 1 5 0 6 8 1
题意:有n个点,m条边,你需要从1-n使得路径长度最短,并且走过的坏路要修,没走到的好路要炸,在最短路径的前提下使得需要的工程量最小。
思路:dp[i]表示到i点需要的工程量,dp[1]=所有的好路数目,中途经过一个好的要-1,经过一个坏的要+1,用bfs来确保是最短路径。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int u,v,next,type;
bool flag;
}edge[200010];
int n,m,Head[100010],pre[200010],dp[100010],tot=1,step[100010];
bool vis[100010],vis2[100010];
queue<int > qu;
void add(int u,int v,int type)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot].type=type;
edge[tot].next=Head[u];
Head[u]=tot;
tot++;
}
int main()
{
int i,j,k,u,v,type,sum=0,ret,a,b;
scanf("%d%d",&n,&m);
add(0,1,1);pre[1]=1;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&type);
add(u,v,type);
add(v,u,type);
sum+=type;
}
dp[1]=sum;
vis2[1]=1;
qu.push(1);
while(!qu.empty())
{
u=qu.front();
qu.pop();
vis[u]=1;vis2[u]=0;
if(u==n)
break;
for(i=Head[u];i>0;i=edge[i].next)
{
v=edge[i].v;
if(vis[v])
continue;
if(step[v]==0 || step[v]>step[u]+1)
{
step[v]=step[u]+1;
if(edge[i].type==1)
dp[v]=dp[u]-1;
else
dp[v]=dp[u]+1;
pre[v]=i;
}
else if(step[v]==step[u]+1)
{
if(edge[i].type==1)
ret=dp[u]-1;
else
ret=dp[u]+1;
if(ret<dp[v])
{
dp[v]=ret;
pre[v]=i;
}
}
if(!vis2[v])
{
qu.push(v);
vis2[v]=1;
}
}
}
printf("%d\n",dp[n]);
v=n;k=0;
while(v!=0)
{
edge[pre[v]].flag=1;
v=edge[pre[v]].u;
}
for(i=1;i<=m;i++)
{
k=i*2;
a=edge[k].type;
b=edge[k].flag+edge[k+1].flag;
if(a!=b)
printf("%d %d %d\n",edge[k].u,edge[k].v,a^1);
}
}