当前位置 : 主页 > 编程语言 > c语言 >

Codeforces Round #287 (Div. 2) E. Breaking Good 最短路&&约束条件

来源:互联网 收集:自由互联 发布时间:2023-09-07
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

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
inputCopy
2 1
1 2 0
outputCopy
1
1 2 1
inputCopy
4 4
1 2 1
1 3 0
2 3 1
3 4 1
outputCopy
3
1 2 0
1 3 1
2 3 0
inputCopy
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
outputCopy
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
题意:无向图;可能有多条最短路,在保证最短路的前提下使得总修理量最小;
如果最短路中有道路没有修好要 repair,除最短路以外的路要炸毁———–总修理量;

在更新最短路的时候,记录路径,然后递归查找路径,
一道好题;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>

typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 500005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

struct Edge {
    int  from, to, z;
};

struct Heapnod {
    int u, d;
    bool operator < (const Heapnod& rhs)const {
        return d > rhs.d;
    }
};

int d[maxn], p[maxn], f[maxn], vis[maxn];
int n, m, sum;
bool finish[maxn];
vector<int> G[maxn];
vector<Edge>edge;
void init() {
    memset(vis, 0, sizeof(vis));
    memset(finish, 0, sizeof(finish));
    memset(d, 31, sizeof(d));
    memset(f, 0, sizeof(f));
    sum = 0;
}

void addedge(int u, int v, int w) {
    edge.push_back(Edge{ u, v, w });
    int k = edge.size();
    G[u].push_back(k - 1);
    if (w)sum++;
}

void dijkstra(int s) {
    priority_queue<Heapnod>q;
    d[s] = 0;
    f[s] = sum / 2;
    p[s] = -1;
    q.push(Heapnod{ s,0 });
    while (!q.empty()) {
        Heapnod x = q.top();
        q.pop();
        int u = x.u;
        if (finish[u])continue;
        finish[u] = true;
        for (int i = 0; i < G[u].size(); i++) {
            Edge e = edge[G[u][i]];
            if (d[e.to] > d[u] + 1) {
                d[e.to] = d[u] + 1;
                if (e.z) {
                    f[e.to] = f[u] - 1;
                }
                else {
                    f[e.to] = f[u] + 1;
                }
                p[e.to] = G[u][i];
                q.push(Heapnod{ e.to,d[e.to] });
            }
            else if (d[e.to] == d[u] + 1) {
                int k;
                if (e.z) {
                    k = f[u] - 1;
                }
                else k = f[u] + 1;
                if (f[e.to] > k) {
                    f[e.to] = k;
                    p[e.to] = G[u][i];
                }
            }
        }
    }
}

void bfs(int n) {
    if (n == -1)return;
    vis[n] = 1;
    bfs(p[edge[n].from]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    init();
    for (int i = 0; i < m; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        addedge(x, y, z);
        addedge(y, x, z);
    }
    dijkstra(1);
    bfs(p[n]);
    cout << f[n] << endl;
    for (int i = 0; i < 2 * m; i+=2) {
        if ((vis[i] || vis[i + 1]) && !edge[i].z) {
            cout << edge[i].from << ' ' << edge[i].to << ' ' << 1 << endl;
        }
        else if (!vis[i] && !vis[i + 1] && edge[i].z) {
            cout << edge[i].from << ' ' << edge[i].to << ' ' << 0 << endl;
        }
    }
}
【感谢龙石为本站提供数据共享交换平台 http://www.longshidata.com/pages/exchange.html】
上一篇:Codeforces Round #451 (Div. 2) E 数论
下一篇:没有了
网友评论