Codeforces Round #287 (Div. 2) E bfs+记录最短路径(邻接表的使用



链接:戳这里


E. Breaking Good
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Examples
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表示该路畅通,0表示该路要修

现在小偷在城市1偷了东西要逃到城市n。逃得路径必须是最短路径,但是最短路径上有些路要修,那么小偷就必须修

然后不是最短路径上的畅通的路,小偷要损毁避免警察来追。

输出小偷在走最短路径的情况下,需要修的路和损毁的路的数目,以及将要修的路和要损毁的路输出,并标记修或毁

比如1->2这条路是要损毁的,那么必须输出1 2 0 ,如果是要修的 输出1 2 1 


思路:

假设当前最短路径已知距离为d,且n个城市之间的畅通的路总量为tot,最短路径上畅通的路数目为x

则满足一个等式:d-x+n-x为要修和损毁的路径总和 ,显然x取越大越好

bfs先跑最短路,当前dis[v]>dis[u]+1,则更新最短路以及路径上的畅通道路数目

当前dis[v]=dis[u]+1,则更新当前路径上的畅通道路数目去max

然后pre[v]=u记录最短路走的路径


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double lb;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n,m;
struct edge{
    int v,next,w,f;
}e[200100];
int head[100100],tot=0,sum=0;
void Add(int u,int v,int w,int f){
    e[tot].v=v;
    e[tot].w=1;
    e[tot].f=f;
    e[tot].next=head[u];
    head[u]=tot++;
}
int dis[100100],num[100100],pre[100100],vis[200100],road[100100],cnt=0;
void print(int x){
    if(x==-1) return ;
    else print(pre[x]);
    road[cnt++]=x;
}
void BFS(int st){
    for(int i=1;i<=n;i++) dis[i]=Max;
    for(int i=1;i<=n;i++) num[i]=Max;
    mst(pre,-1);
    mst(vis,0);
    queue<int> qu;
    qu.push(st);
    dis[st]=0;
    num[st]=0;
    while(!qu.empty()){
        int u=qu.front();
        qu.pop();
        vis[u]=1;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            int w=e[i].w;
            int f=e[i].f;
            if(!vis[v] && dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                num[v]=num[u]+f;
                qu.push(v);
                pre[v]=u;
            } else if(!vis[v] && dis[v]==dis[u]+w){
                if(num[v]<num[u]+f) {
                    num[v]=num[u]+f;
                    pre[v]=u;
                }
            }
        }
    }
    //for(int i=1;i<=n;i++) printf("%d\n",dis[i]);
    //for(int i=1;i<=n;i++) printf("%d\n",num[i]);
    printf("%d\n",dis[n]+sum-2*num[n]);
    print(n);
    mst(vis,0);
    //for(int i=0;i<cnt;i++) cout<<road[i]<<" ";cout<<endl;
    for(int i=0;i<cnt-1;i++){
        int u=road[i];
        int v=road[i+1];
        for(int j=head[u];j!=-1;j=e[j].next){
            if(e[j].v==v) {
                if(e[j].f==0) e[j].f=e[j^1].f=2;
                else e[j].f=e[j^1].f=0;
                break;
            }
        }
    }
    for(int i=0;i<tot;i++){
        if(e[i].f==0) continue;
        printf("%d %d %d\n",e[i^1].v,e[i].v,e[i].f-1);
        i++;
    }
}
int main(){
    mst(head,-1);
    scanf("%d%d",&n,&m);
    sum=0;
    for(int i=1;i<=m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        Add(x,y,1,z);
        Add(y,x,1,z);
        sum+=z;
    }
    BFS(1);
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值