PAT (Top Level) Practise 1001 Battle Over Cities - Hard Version (35)
3833 단어 pat
시간 제한
800 ms
메모리 제한
65536 kB
코드 길이 제한
8000 B
판정 절차
Standard
작자
CHEN, Yue
It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the minimum cost. On the other hand, if losing a city will cost us too much to rebuild the connection, we must pay more attention to that city.
Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to point out the city to which we must pay the most attention.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N (<=500), and M, which are the total number of cities, and the number of highways, respectively. Then M lines follow, each describes a highway by 4 integers:
City1 City2 Cost Status
where City1 and City2 are the numbers of the cities the highway connects (the cities are numbered from 1 to N), Cost is the effort taken to repair that highway if necessary, and Status is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.
Note: It is guaranteed that the whole country was connected before the war.
Output Specification:
For each test case, just print in a line the city we must protest the most, that is, it will take us the maximum effort to rebuild the connection if that city is conquered by the enemy.
In case there is more than one city to be printed, output them in increasing order of the city numbers, separated by one space, but no extra space at the end of the line. In case there is no need to repair any highway at all, simply output 0.
Sample Input 1:
4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 1 0
Sample Output 1:
1 2
Sample Input 2:
4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 2 1
Sample Output 2:
0
간단하게 말하면 각 점의 최소 생성 트리를 일일이 삭제하는 데 가장 많은 비용이 든다는 것이다. 연결이 되지 않으면 반드시 가장 큰 것이다. 마지막으로 답안을 반복해서 출력하면 된다.
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<stack>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
const int maxn = 1e5 + 10;
const int INF = 0x7FFFFFFF;
int n, m, fa[maxn], cost[maxn];
int get(int x)
{
return x == fa[x] ? x : fa[x] = get(fa[x]);
}
struct point
{
int x, y, c, u;
void read(){ scanf("%d%d%d%d", &x, &y, &c, &u); }
bool operator<(const point&a)const{ return u == a.u ? c<a.c : u>a.u; }
}a[maxn];
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = 0; i < m; i++) a[i].read();
sort(a, a + m);
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++) fa[j] = j;
int cnt = n - 2;
cost[i] = 0;
for (int j = 0; j < m; j++)
{
if (a[j].x == i || a[j].y == i) continue;
int fx = get(a[j].x), fy = get(a[j].y);
if (fx == fy) continue; cnt--;
fa[fx] = fy; if (!a[j].u) cost[i] += a[j].c;
}
if (cnt > 0) cost[i] = INF;
ans = max(ans, cost[i]);
}
if (ans)
{
int flag = 0;
for (int i = 1; i <= n; i++)
if (cost[i] == ans)
{
printf("%s%d", flag ? " " : "", i); flag = 1;
}
}
else printf("%d
", ans);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
PAT 01-2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.