Movement Regulations (24) - Check and Collect Basic Questions - Imprison Criminals

【Problem Description】

There are currently two prisons in City S, which hold N criminals in total, numbered 1~N. The relationship between them is naturally very

Discord. Many criminals even have long-standing grievances, and conflicts may break out at any time if objective conditions are met. We use "complain

Qi value" (a positive integer value) to represent the degree of hatred between two criminals, the greater the resentment value, the greater the relationship between the two criminals

The more grievances between the two. If two criminals with grievance value c are imprisoned in the same prison, there will be friction between them and cause a conflict event with influence c. At the end of each year, the police station will arrange all the conflicts in the prison in this year in descending order of influence into a list, and then report it to the mayor of Z in S City. Mayor Z, who is busy with official duties, will only look at the impact of the first event on the list. If the impact is bad, he will consider replacing the police chief. After examining in detail the contradictory relationship among the N criminals, the chief of police felt under great pressure. He is going to redistribute the criminals in the two prisons so that the impact of the conflicts will be less, so as to keep his black hat. Assuming that as long as there is animosity between certain two criminals in the same prison, then there must be friction between them at some point every year. So how should the criminals be distributed so that the impact of the conflict seen by Mayor Z is the least? What is this minimum value?

【enter】

The input file name is prison.in. Two numbers are separated by a space on each line of the input file. The first row contains two positive integers N and M, respectively representing the number of criminals and the number of pairs of criminals who have hatred. Each of the next M lines contains three positive integers aj, bj, cj, indicating that there is hatred between criminals aj and bj, and their resentment value is cj. Data guarantee 1<=aj<=bj<=N, 0<=cj<=1000000000, and each pair of criminals only appears once.

【Output】

The output file prison.out has a total of 1 line, which is the impact of the conflict seen by Mayor Z. If this year prison

No conflicts occurred in , please output 0.

【Input and output example】

4 6

1 4 2534

2 3 3512

1 2 28351

1 3 6618

2 4 1805

3 4 12884

prison.out

3512

【Input and output sample description】

The grievance value among criminals is shown in the left picture below, the distribution method of criminals is shown in the right picture, and the conflict events seen by the mayor

The influence is 3512 (caused by criminals 2 and 3). No other division method will be better than this division method.

【data range】

For 30% of the data there were N ≤ 15.

For 70% of the data have N≤2000, M≤50000.

For 100% of the data there are N≤20000 and M≤100000.

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int n, m, f[40001], vis[40001], x, y, z;
struct data
{
    int a, b, c;
} e[100001];
int comp(data a, data b)
{
    return a.c > b.c;
}
int find(int x)
{
    return f[x] == x ? x : f[x] = find(f[x]);
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> e[i].a >> e[i].b >> e[i].c;
    for (int i = 1; i <= n; i++)
        f[i] = i;
    sort(e + 1, e + m + 1, comp);
    for (int i = 1; i <= m; i++)
    {
        x = find(e[i].a);
        y = find(e[i].b);
        if (x == y)
        {
            cout << e[i].c;
            return 0;
        }
        else
        {
            if (vis[e[i].a])
            {
                z = find(vis[e[i].a]);
                f[y] = z;
            }
            else
                vis[e[i].a] = e[i].b;

            if (vis[e[i].b])
            {
                z = find(vis[e[i].b]);
                f[x] = z;
            }
            else
                vis[e[i].b] = e[i].a;
        }
    }
    cout << 0;
    return 0;
}

Guess you like

Origin blog.csdn.net/hdq1745/article/details/127033954