标签:div nes apt 分享图片 each com load table sed
ICPC (Isles of Coral Park City) consist of several beautiful islands.
The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.
The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.
However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.
As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.
Write a program that advises the mayor which bridges are no alternative bridges for the given input.
The input consists of several tests case.
Each line of the next M lines contains three integers Si, Di and Ci (1?≤?i?≤?M) which represent that it will cost Ci to build the bridge between islands Si and Di. You may assume 3?≤?N?≤?500, N???1?≤?M?≤?min(50000,?N(N???1)/2), 1?≤?Si?<?Di?≤?N, and 1?≤?Ci?≤?10000. No two bridges connect the same pair of two islands, that is, if i?≠?j and Si?=?Sj , then Di?≠?Dj. If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.
Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.
4 4 1 2 3 1 3 3 2 3 3 2 4 3 4 4 1 2 3 1 3 5 2 3 3 2 4 3 4 4 1 2 3 1 3 1 2 3 3 2 4 3 3 3 1 2 1 2 3 1 1 3 1
1 3 3 9 2 4 0 0
题意是建桥,然后求最小建桥方案中哪些桥是必须要留着的,求这些桥的个数和总花费
先求出最小生成树,然后再去掉一条条边,看哪些边去掉后结果和最小生成树的结果不一样,那么这些边就是要留着的
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<string> #include<vector> #include<cmath> #define debug(a) cout << #a << " " << a << endl using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const ll maxn = 1e5 + 10; int n, m, num, cnt, result, pre[maxn], b[maxn], vis[maxn]; struct node { int x, y, z; }; node edge[maxn]; bool cmp( node p, node q ) { return p.z < q.z; } void init() { for( int i = 1; i <= n; i ++ ) { pre[i] = i; } } int find( int x ) { int r = x; while( r != pre[r] ) { r = pre[r]; } int i = x, j; while( pre[i] != r ) { j = pre[i]; pre[i] = r; i = j; } return r; } void join( int x, int y ) { int fx = find(x), fy = find(y); if( fx != fy ) { pre[fx] = fy; } } int kruskal( int flag ) { int sum = 0; for( int i = 0; i < m; i ++ ) { if( vis[i] ) { continue; } int fx = find( edge[i].x ); int fy = find( edge[i].y ); if( fx != fy ) { sum += edge[i].z; pre[fx] = fy; if( !flag ) { b[cnt++] = i; } } } return sum; } int main() { std::ios::sync_with_stdio(false); while( cin >> n >> m ) { memset( vis, 0, sizeof(vis) ); for( int i = 0; i < m; i ++ ) { cin >> edge[i].x >> edge[i].y >> edge[i].z; } sort( edge, edge + m, cmp ); cnt = 0, num = 0, result = 0; init(); int ans = kruskal(0); for( int i = 0; i < cnt; i ++ ) { init(); vis[b[i]] = 1; if( kruskal(1) != ans ) { result += edge[b[i]].z; num ++; } vis[b[i]] = 0; } cout << num << " " << result << endl; } return 0; }
There is No Alternative CSU - 2097 最小生成树
标签:div nes apt 分享图片 each com load table sed
原文地址:https://www.cnblogs.com/l609929321/p/9021474.html