Crowd Control

版权声明:转载请注明出处,欢迎讨论,共同进步,QQ:1051780721 https://blog.csdn.net/ACM2017/article/details/82027487

4895: Crowd Control

时间限制: 1 Sec 内存限制: 128 MB

题目描述

The BAPC draws a large number of visitors to Amsterdam. Many of these people arrive at the train station, then walk from intersection to intersection through the streets of Amsterdam in a big parade until they reach the BAPC location.

A street can only allow a certain number of people per hour to pass through. This is called the capacity of the street. The number of people going through a street must never exceed its capacity, otherwise accidents will happen. People may walk through a street in either direction.

The BAPC organizers want to prepare a single path from train station to BAPC location. They choose the path with maximum capacity, where the capacity of a path is defined to be the minimum capacity of any street on the path. To make sure that nobody walks the wrong way, the organizers close down the streets which are incident
1 to an intersection on the path,but not part of the path.

Can you write a program to help the organizers decide which streets to block? Given a graph of the streets and intersections of Amsterdam, produce the list of streets that need to be closed down in order to create a single maximum-capacity path from the train station to the BAPC. The path must be simple, i.e. it may not visit any intersection more than once.

输入

• The first line contains two integers: n, the number of intersections in the city, and m,the number of streets (1 ≤ n, m ≤ 1000).
• The following m lines each specify a single street. A street is specified by three integers, ai, bi and ci, where ai and bi are ids of the two intersections that are connected by this street (0 ≤ ai, bi < n) and ci is the capacity of this street (1 ≤ ci ≤ 500000). Streets are numbered from 0 to m − 1 in the given order.
You may assume the following:
• All visitors start walking at the train station which is the intersection with id 0. The BAPC is located at the intersection with id n − 1.
• The intersections and streets form a connected graph.
• No two streets connect the same pair of intersections.
• No street leads back to the same intersection on both ends.
• There is a unique simple path of maximum capacity.

输出

Output a single line containing a list of space separated street numbers that need to be blocked in order to create a single maximum-capacity path from train station to BAPC. Sort these street numbers in increasing order.
If no street must be blocked, output the word “none” instead.

样例输入

7 10
0 1 800
1 2 300
2 3 75
3 4 80
4 5 50
4 6 100
6 1 35
0 6 10
0 2 120
0 3 100

样例输出

0 2 4 6 7 8

来源/分类

BAPC2017 Preliminaries

Solution

最后要保留的边一定在最大生成树上。

事实证明图论题必须很细心

#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
struct Node {
    int a, b, id, val;
} s[N];
struct data {
    int id, v, nxt;
} e[N<<1];
struct edge {
    int u, pre, id;
} Que[N];
int fa[N], ans[N], head[N];
int n, m, tot, cnt, l, r;
bool vis[N], yi[N];//yi: edges preserved;

bool cmp(Node a, Node b) {//权值从大到小排序
    return a.val > b.val;
}

int Find(int x) {
    if (fa[x] != x) fa[x] = Find(fa[x]);
    return fa[x];
}

bool Check(int x, int y) {
    int fx = Find(x);
    int fy = Find(y);
    return fx == fy;
}

void Kruskal(int n, int m) {
    int fx, fy;
    sort(s, s + m, cmp);
    for (int i = 0; i < m; ++i) {
        fx = Find(s[i].a);
        fy = Find(s[i].b);
        if (fx != fy) {
            ans[tot++] = i;
            fa[fx] = fy;
        }
        if (Check(0, n - 1)) break;
    }
}

int main() {
    //freopen("../in","r",stdin);
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++i) {
        scanf("%d%d%d", &s[i].a, &s[i].b, &s[i].val);
        s[i].id = i;
    }
    for (int i = 0; i < n; ++i) fa[i] = i;
    Kruskal(n, m);
    //for (int i=0;i<tot;++i) printf("%d %d %d\n",ans[i],s[ans[i]].a,s[ans[i]].b);
    cnt = 1;
    for (int i = 0; i < tot; ++i) {
        e[cnt] = (data) {
                ans[i], s[ans[i]].b, head[s[ans[i]].a]
        };
        head[s[ans[i]].a] = cnt++;
        e[cnt] = (data) {
                ans[i], s[ans[i]].a, head[s[ans[i]].b]
        };
        head[s[ans[i]].b] = cnt++;
    }
    l = -1;
    Que[0].u = 0;
    vis[0] = true;
    edge now, nxx;
    while (l < r) {
        now = Que[++l];
        for (int i = head[now.u]; i; i = e[i].nxt) {
            nxx.u = e[i].v;
            if (!vis[nxx.u]) {
                nxx.pre = l;
                nxx.id = e[i].id;
                vis[nxx.u] = true;
                Que[++r] = nxx;
            }
        }
    }
    int i;
    for (i = r; i >= 0; --i)
        if (Que[i].u == n - 1) break;
    memset(vis,0,sizeof(vis));
    vis[0] = true;
    while (i) {
        //printf("%d %d\n",s[Que[i].id].a,s[Que[i].id].b);
        vis[s[Que[i].id].b] = vis[s[Que[i].id].a] = yi[Que[i].id] = true;
        i = Que[i].pre;
    }
    tot = 0;
    for (i = 0; i < m; ++i) {
        if ((vis[s[i].a] || vis[s[i].b]) && !yi[i]) ans[tot++] = s[i].id;
    }
    if (tot == 0) printf("none\n");
    else {
        sort(ans,ans+tot);
        for (i = 0; i < tot - 1; ++i) printf("%d ", ans[i]);
        printf("%d\n", ans[tot - 1]);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/ACM2017/article/details/82027487