原图可能有多个连通分量,先DFS找出每个连通分量中最小节点,这些必然是要攻占的城市。

设 n 为节点数, m 为边数, cnt 为初始连通分量数,在剩下的边数不小于 m - (n - cnt) 的时候,图的连通性是不变的,也就是在这之前可以适当策略删边保持结果不变。

当边数小于等于 m - (n - cnt) 时,每删一条边,必然多一个连通分量,我们总可以做到让多出来这个连通分量的最小结点 是所有节点中除去已经选定的那些节点之外的最小节点,所以这时对节点以权值排序从小往大记到删够边数为止。

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
typedef long long LL;
const int maxn = ;
const int maxm = ;
int n, m, k;
int fst[maxn];
int wt[maxn];
int vis[maxn];
int nex[maxm], w[maxm], ntp;
void AddEdge(int a, int b)
{
nex[ntp] = fst[a];
w[ntp] = b;
fst[a] = ntp ++;
}
inline int min(int a, int b){return a < b ? a : b;}
void DFS(int nd, int &okcity)
{
if(vis[nd]) return;
vis[nd] = true;
if(okcity == - || wt[nd] < wt[okcity])
okcity = nd;
for(int i = fst[nd]; i != -; i = nex[i])
DFS(w[i], okcity);
}
int main()
{
int t, ca;
for(scanf("%d", &t), ca = ; ca <= t; ca ++)
{
int a, b;
scanf("%d%d%d", &n, &m, &k);
memset(fst, -, sizeof(fst));
memset(vis, , sizeof(vis));
for(int i = ; i <= n; i ++)
scanf("%d", &wt[i]);
ntp = ;
for(int i = ; i < m; i ++)
{
scanf("%d%d", &a, &b);
AddEdge(a, b);
AddEdge(b, a);
}
int cnt = ;
LL ans = ;
for(int i = ; i <= n; i ++)
{
int okcity = -;
DFS(i, okcity);
if(okcity != -)
cnt ++, ans += wt[okcity], vis[okcity] = ;
} if((k -= m - (n - cnt)) > )
{
int i, j;
for(i = , j = ; i <= n; i ++)
if(vis[i] != ) wt[j ++] = wt[i];
std::sort(wt + , wt + j);
for(int i = ; k > && i <= j; i ++)
ans += wt[i], k --;
}
printf("Case #%d: %lld\n", ca, ans);
}
return ;
}

hihoCoder 1160 攻城略地的更多相关文章

  1. 【占位】HihoCoder 1160 : 攻城略地(并查集好题)

    攻城略地 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 A.B两国间发生战争了,B国要在最短时间内对A国发动攻击.已知A国共有n个城市(城市编号1, 2, …, n),城 ...

  2. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  3. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  4. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  5. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  6. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  7. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  8. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

  9. 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切

      题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词   代码注意点: 1. getline(istre ...

随机推荐

  1. MFC加载皮肤 转自:http://www.cctry.com/thread-4032-1-1.html

    VC皮肤库SkinSharp 1.0.6.6的使用: SkinSharp又称Skin#,是Windows环境下一款强大的换肤组件.SkinSharp作为换肤控件,只需要在您的程序中添加一行代码,就能让 ...

  2. lucas定理,组合数学问题

    对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...

  3. HDU2191多重背包例题

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d ...

  4. 使用XmlWriter写Xml

    假定创建了XmlWriter的实例变量xmlWriter,下文中将使用此实例变量写Xml 1.如何使用XmlWriter写Xml文档声明 ? // WriteStartDocument方法可以接受一个 ...

  5. 如何用adb logcat保存日志

    //将log 保存到当前目录下 adb logcat -v time >a.log //log过滤 adb logcat | grep MyAppName //清除log adb logcat ...

  6. 解决虚拟机 正在决定eht0 的ip信息失败 无链接-- 虚拟机上linux redhat 上网问题

    对于虚拟机上,linux redhat上网的配置方式有三种 一.用setup命令进行配置(具体技巧可查setup命令的使用) 二.直接用 ifconfig eth0  ip地址进行配置 三.进入系统文 ...

  7. 4.python函数基础

    一.函数 1.函数简介 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但 ...

  8. codeforces B. Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...

  9. ios xcode Could not load the "MyImage.png" image referenced from a nib in the bundle with identifier "com.mytest.MyProject"

    出现找不到xib指定的图片,需要指定图片的完整路径,不能只是图片名 详见:http://vocaro.com/trevor/blog/2012/10/21/xcode-groups-vs-folder ...

  10. CSRF和XSS

    XSS(跨站脚本攻击): 攻击者发现XSS漏洞——构造代码——发送给受害人——受害人打开——攻击者获取受害人的cookie——完成攻击 XSS是什么?它的全名是:Cross-site scriptin ...