HDU 3080 The plan of city rebuild
来源:互联网 发布:淘宝儿童服装店哪个好 编辑:程序博客网 时间:2024/05/16 09:28
这道题用的是最小生成树中的kruskal算法,这题输入比较烦, 要注意去掉的点的处理。
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#define mem(a) memset(a, 0, sizeof(a))using namespace std;int father[10005], son[10005], nump, nume, numnp, numne, numds;bool vis[10005];struct eg{int a;int b;int egv;}e[60000];bool cmp(const eg & a, const eg & b) { return a.egv < b.egv; } int research(int a){if(a == father[a])return a;else{research(father[a]);}}bool join(int a, int b){int root1, root2;root1 = research(a);root2 = research(b);if(root1 == root2)return false;else if(son[root1] >= son[root2]){ father[root2] = root1; son[root1] += son[root2];}else{ father[root1] = root2; son[root2] += son[root1];}return true;}int main(int argc, char *argv[]){int t, i, j, k, flag, num, nn;long long ans;scanf("%d", &t);while(t--){nn = 0;num = 0;ans = 0;flag = 0;mem(father);mem(son);mem(vis);mem(e);scanf("%d%d", &nump, &nume);for(i = 0;i < nume;i++){scanf("%d%d%d",&e[i].a, &e[i].b, &e[i].egv);}scanf("%d%d", &numnp, &numne);for(i = nume;i < (nume + numne);i++){scanf("%d%d%d",&e[i].a, &e[i].b, &e[i].egv);}scanf("%d",&numds);for(i = 0;i < numds;i++){scanf("%d",&j);vis[j] = 1;}for(i = 0;i < (nume + numne);i++){if(vis[e[i].a] == 1||vis[e[i].b] == 1)e[i].egv = -1, nn++;}sort(e, e + nume + numne, cmp);for(i = 0;i < (nump + numnp);i++){father[i] = i;son[i] = 1;}for(i = nn;i < (nume + numne);i++){ if(join(e[i].a, e[i].b)) { num++; ans += e[i].egv; } if(num == (nump + numnp - numds - 1)) { flag = 1; break; }}if(flag == 1)printf("%lld\n", ans);elseprintf("what a pity!\n");}return 0;}
0 0
- hdu 3080 The plan of city rebuild
- HDU 3080 The plan of city rebuild
- HDU 3080 The plan of city rebuild(prim和kruskal)
- HDU 3080 The plan of city rebuild(除点最小生成树)
- hdoj 3080 The plan of city rebuild(prim)
- hdu3080 The plan of city rebuild
- hdu3080The plan of city rebuild
- hdu3080 The plan of city rebuild(最小生成树)
- The Design of City Car
- The Plan Of Reading Books
- The Plan of next Project
- In the exotic view of the city
- 中级3 The city of song
- Analysis the execution plan of Sybase
- plan for the first term of junior
- The Prisoner of Zenda——11、A dangerous plan
- Rebuild the application tablespace
- Rebuild the controlfile
- CKRule BRMS-决策表使用说明
- Java_语法基础_“$”惹的祸
- java导出excel
- Swift自定义控件--输入框
- java语言IO流之FileReader
- HDU 3080 The plan of city rebuild
- Java socket多线程
- 程序员修炼之路-(0)前言:在路上, 再上路
- java语言编程之FileWriter
- Java导出PDF文件
- 接口
- Android SDK 国内镜像
- 从键盘输入数据
- LeetCode Excel Sheet Column Number