CSUOJ 1116 Kingdoms
来源:互联网 发布:算法就是程序 编辑:程序博客网 时间:2024/06/03 09:01
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116、
1116: Kingdoms
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 441 Solved: 145
Description
A kingdom has n cities numbered 1 to n, and some bidirectional roads connecting cities. The capital is always city 1.
After a war, all the roads of the kingdom are destroyed. The king wants to rebuild some of the roads to connect the cities, but unfortunately, the kingdom is running out of money. The total cost of rebuilding roads should not exceed K.
Given the list of m roads that can be rebuilt (other roads are severely damaged and cannot be rebuilt), the king decided to maximize the total population in the capital and all other cities that are connected (directly or indirectly) with the capital (we call it "accessible population"), can you help him?
Input
The first line of input contains a single integer T (T<=20), the number of test cases.
Each test case begins with three integers n(4<=n<=16), m(1<=m<=100) and K(1<=K<=100,000).
The second line contains n positive integers pi (1<=pi<=10,000), the population of each city.
Each of the following m lines contains three positive integers u, v, c (1<=u,v<=n, 1<=c<=1000), representing a destroyed road connecting city u and v, whose rebuilding cost is c.
Note that two cities can be directly connected by more than one road, but a road cannot directly connect a city and itself.
Output
For each test case, print the maximal accessible population.
Sample Input
24 6 6500 400 300 2001 2 41 3 31 4 24 3 52 4 63 2 74 6 5500 400 300 2001 2 41 3 31 4 24 3 52 4 63 2 7
Sample Output
11001000
HINT
Source
湖南省第八届大学生计算机程序设计竞赛
题目大意:
有编号1到n的n个城市和m条道路,编号为1的城市为首都,每座城市有Pi(1 <= i <= n)的人口,一共有K的预算,修复每条道路来连接城市u和v需要c的费用,在总费用不超过预算的前提下,要求让城市与首都连通使这些与首都连通的城市(包括首都)的人口尽可能的多。
解题思路:
很明显的看出有个最小生成树,但是还要求人口最多(不一定最后所有城市都可以与首都连通),状态压缩dp,n的范围不超过16,用int的二进制,二进制的2^x位为1表示选择第x + 1个城市来构造最小生成树。
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;struct Edge{ Edge(int x = 0, int y = 0, int Weight = 0) : x(x), y(y), Weight(Weight){ } int x; int y; int Weight; bool operator < (const Edge& b) const{ if(this->Weight < b.Weight) return true; else if(this->Weight == b.Weight){ if(this->x < b.x) return true; else if(this->x == b.x) return this->y < b.y; else return false; }else return false; }};void ReSet(int arr[], int n){ for(int i = 1; i <= n; i++) arr[i] = i;}int FindFather(int arr[], int x){ if(arr[x] == x) return x; return arr[x] = FindFather(arr, arr[x]);}int Merge(int arr[], int a, int b){ int fa = FindFather(arr, a), fb = FindFather(arr, b); if(fa != fb) return arr[fb] = arr[fa]; return -1;}int T, n, m, K, u, v, c, cost, sum, ans, x, y;int p[17], F[17];Edge E[101];int main(){ scanf("%d", &T); for(int t = 1; t <= T; t++) { scanf("%d%d%d", &n, &m, &K); for(int i = 1; i <= n; i++) scanf("%d", &p[i]); for(int i = 0; i < m; i++){ scanf("%d%d%d", &u, &v, &c); E[i] = Edge(u, v, c); } sort(E, E + m); //对边按权值从小到大排序 ans = 0; for(int i = 3; i <= (1 << n) - 1; i++){ if((i & 1) == 0) continue; //去掉不含首都的状态 ReSet(F, n); sum = p[1]; cost = 0; for(int j = 0; j < m; j++){ //最小生成树 if(((1 << (E[j].x - 1)) & i) && ((1 << (E[j].y - 1)) & i) && FindFather(F,E[j].x) != FindFather(F, E[j].y)){ Merge(F, E[j].x, E[j].y); cost += E[j].Weight; } } for(int j = 2; j <= n; j++){ //判断选中的城市是否都与首都连通 if(((1 << (j - 1)) & i) && FindFather(F, j) != FindFather(F, 1)){cost = 100001; break;} } if(cost <= K){ //花费不超过预算且所有被选中的城市都与首都连通则计算该状态下的人口 for(int j = 2; j <= n; j++) if((1 << (j - 1)) & i) sum += p[j]; if(sum > ans) ans = sum; } } printf("%d\n", ans); } return 0;}
0 0
- CSUOJ 1116 Kingdoms
- 1116: Kingdoms+csuoj+暴力枚举+最小生成树
- CSU 1116 Kingdoms
- CSU 1116 Kingdoms
- Kingdoms
- CSUOJ
- CSUOJ
- CSU 1116: Kingdoms(状态压缩+DP)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- csu 1116 Kingdoms 最小生成树 二进制枚举
- Kingdoms UVA
- csuoj--1307
- csuoj Lexicography
- csuoj X
- 1989CSUOJ
- hdu 3442 Three Kingdoms
- FZU - 1981 Three kingdoms
- Sicily 1914. Kingdoms
- POJ 2299 Ultra-QuickSort [树状数组做法]
- 杭电 1166 敌兵布阵 (线段树)
- ReactNative之TextInput
- refresh的停车场
- How to fix a "Duplicated Symbols" error on binary files
- CSUOJ 1116 Kingdoms
- iOS APP性能优化
- dubbo-demo+简易监控中心安装+管理控制台安装
- viewpager实现图片轮播+小圆点跟着动
- C++头文件的一些笔记
- SQL_数据库基础试题(培训/已完结)
- 909422229__Mysql查询语句基本用法总结
- 程序猿媳妇儿注意事项
- stm32通过SPI与adc124s021通信读取ad