最短路Codeforces E. Breaking Good

一款名为BreakingGood的游戏要求玩家帮助主角Walter规划从城市1到城市n的最短且受影响道路最少的逃亡路径,玩家需要考虑双向道路的工作状态来制定最佳方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Breaking Good

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


需要修改的尽量少,那么就要最短路上的尽量是1,

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int INF=1000000000;
int N,M,num;
struct Edge
{
    int u,v,type,next,f,id;
}edge[maxn*2];
int dis[maxn],head[maxn];
int pre[maxn],cnt[maxn];

int vis[maxn],id[maxn];
void init()
{
    num=0;
    memset(head,-1,sizeof(head));
    memset(cnt,0,sizeof(cnt));
    memset(pre,-1,sizeof(pre));
}
void add_edge(int u,int v,int type,int id)
{
    edge[num].v=v;
    edge[num].u=u;
    edge[num].next=head[u];
    edge[num].type=type;
    edge[num].f=1;
    edge[num].id=id;
    head[u]=num++;
}
void SPFA(int s,int t)
{
    for(int i=0;i<=N;i++)
    {
        dis[i]=INF;
        vis[i]=0;
    }
    queue<int> q;
    q.push(s);
    vis[s]=1;
    dis[s]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].f;
            if((dis[v]>dis[u]+w)||(dis[v]==dis[u]+1&&cnt[v]<cnt[u]+(edge[i].type)))
            {
                dis[v]=dis[u]+w;
                pre[v]=u;
                id[v]=edge[i].id;
                cnt[v]=cnt[u];
                cnt[v]+=edge[i].type;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
int vis_edge[maxn*2];
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        init();
        int u,v,type;
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d%d",&u,&v,&type);
            add_edge(u,v,type,i);
            add_edge(v,u,type,i);
        }
        memset(id,0,sizeof(id));
        SPFA(1,N);
        memset(vis,0,sizeof(vis));
        memset(vis_edge,0,sizeof(vis_edge));
        u=N;
        while(pre[u]!=-1)
        {
            vis_edge[id[u]]=1;
            u=pre[u];
        }
        int ans = 0;
        for (int u = 1; u <= N; ++u)
        {
            for (int i = head[u]; ~i; i = edge[i].next)
            {
                int v = edge[i].v;
                if (!vis_edge[edge[i].id] && edge[i].type && !vis[edge[i].id])
                {
                    ++ans;
                    vis[edge[i].id] = 1;
                }
                else if (vis_edge[edge[i].id] && !edge[i].type && !vis[edge[i].id])
                {
                    ++ans;
                    vis[edge[i].id] = 1;
                }
            }
        }
        memset (vis, 0, sizeof(vis));
        printf("%d\n", ans);
        for (int u = 1; u <= N; ++u)
        {
            for (int i = head[u]; ~i; i = edge[i].next)
            {
                int v = edge[i].v;
                if (vis[edge[i].id])
                    continue;
                if (!vis_edge[edge[i].id] && edge[i].type)
                {
                    printf("%d %d 0\n", u, v);
                    vis[edge[i].id]=1;
                }
                else if (vis_edge[edge[i].id] && !edge[i].type)
                {
                    printf("%d %d 1\n", u, v);
                    vis[edge[i].id]=1;
                }
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值