先上题目:

Domestic Networks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 732   Accepted: 204   Special Judge

Description

Alex is a system administrator of Domestic Networks Inc. His network connects apartments and spans over multiple buildings.

The network expands and Alex has to design a new network segment. He has a map that shows apartments to connect and possible links. Each link connects two apartments and for each possible link its length is known. The goal is to make all apartments connected (possibly through other apartments).

Domestic Networks Inc. buys cable in the nearest cable shop. Unfortunately, shop sells only category 5 and 6 cables at price of p5 and p6 rubles per meter respectively. Moreover, there are only q5 meters of category 5 cable and q6 meters of category 6 cable available in the shop.

Help Alex to solve a hard problem: make a new network construction plan with possible minimal cost. A plan consists of list of links to be made and cable category for each link (each link should be a single piece of cable of either 5 or 6 category). The cost of the plan is the sum of cost of all cables. The total length of cables of each category used in the plan should not exceed the quantity of the cable available in the shop.

Input

The first line of the input file contains two numbers: n — the number of apartments to be connected and m — the number of possible links (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10 000).

Following m lines contain possible link descriptions. Each description consists of three integer numbers: ai and bi — apartments that can be connected by the link and li — link length in meters (0 ≤ li ≤ 100). Apartments are numbered from 1 to n.

The last line of the input file contains four integer numbers: p5q5p6 and q6 — price and quantity of category 5 and 6 cables respectively (1 ≤ piqi ≤ 10 000).

Output

If all apartments can be connected with the available cable, output n lines — an optimal network construction plan. The first line of the plan must contain plan’s cost. Other lines of the plan must consist of two integer numbers each: ai — number of the link to make and ci — the category of the cable to make it of. Links are numbered from 1 to m in the order they are specified in the input file. If there are more than one optimal plans, output any of them.

If there is no plan meeting all requirements, output a single word “Impossible”.

Sample Input

6 7
1 2 7
2 6 5
1 4 8
2 3 5
3 4 5
5 6 6
3 5 3
2 11 3 100

Sample Output

65
1 5
2 6
4 6
5 6
7 5

Source

Northeastern Europe 2007, Northern Subregion
题意:给你一幅图,图上每一条边有长度,给你一定数量的两种电线,告诉你它们的数量以及单价,问你能不能用这些电线花最小的代价令图上每个点互相连通。如果可以就输出最小代价,并输出选了哪些线路,它们分别用了哪种电线,一条线路上只能用一种电线。
做法是先对这个图求一次MST,如果不存在MST,就输出"Impossbile",否则就有可能存在合法的方案,这里我们可以用背包选电线。我们的目标是让尽量多的电线使用便宜的那一种电线,所以我们对便宜的那种电线进行一次背包,剩下的线路用贵的电线,然后判断一下是否符合输出的电线数量,如果符合就输出所有结果,否者就是非法的。
上代码:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <set>
  5 #define MAX 10002
  6 using namespace std;
  7
  8 typedef struct edge{
  9     int u,v,l,id;
 10
 11     bool operator < (const edge& o)const{
 12         return l<o.l;
 13     }
 14 }edge;
 15
 16 edge e[MAX];
 17 int p[MAX],mst[MAX],tot;
 18 int n,m,p5,q5,p6,q6,i5,i6;
 19 int pag[MAX];
 20 bool f[MAX];
 21 set<int> e5,e6;
 22
 23 int findset(int u){
 24     return u==p[u] ? p[u] : p[u]=findset(p[u]);
 25 }
 26
 27 void MST(){
 28     tot=0;
 29     int u,v;
 30     for(int i=0;i<m;i++){
 31         u=findset(e[i].u);   v=findset(e[i].v);
 32         if(p[u]!=p[v]){
 33             p[v]=u;
 34             mst[tot++]=i;
 35         }
 36     }
 37 }
 38
 39 int main()
 40 {
 41     int I5,I6;
 42     //freopen("data.txt","r",stdin);
 43     while(scanf("%d %d",&n,&m)!=EOF){
 44         for(int i=1;i<=n;i++) p[i]=i;
 45         for(int i=0;i<m;i++){
 46             scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].l);
 47             e[i].id=i+1;
 48         }
 49         scanf("%d %d %d %d",&p5,&q5,&p6,&q6);
 50         i5=5;   i6=6;
 51         if(p5>p6){
 52             swap(p5,p6);
 53             swap(q5,q6);
 54             swap(i5,i6);
 55         }
 56         sort(e,e+m);
 57         MST();
 58         if(tot!=n-1){
 59             printf("Impossible\n");
 60             continue;
 61         }
 62
 63         /*********pag*********/
 64         memset(f,0,sizeof(f));
 65         e5.clear(); e6.clear();
 66         //memset(pag,0,sizeof(pag));
 67         f[0]=1;
 68         for(int i=0;i<tot;i++){
 69             int l=e[mst[i]].l;
 70             e6.insert(mst[i]);
 71             for(int j=q5;j>=l;j--){
 72                 if(!f[j] && f[j-l]){
 73                     f[j]=1;
 74                     pag[j]=mst[i];
 75                 }
 76             }
 77         }
 78         int k=q5;
 79         while(!f[k]) k--;
 80         while(k>0){
 81             e5.insert(pag[k]);
 82             e6.erase(pag[k]);
 83             k-=e[pag[k]].l;
 84         }
 85         I5=I6=0;
 86         for(set<int>::iterator it = e5.begin();it!=e5.end();it++){
 87             I5+=e[*it].l;
 88         }
 89         for(set<int>::iterator it = e6.begin();it!=e6.end();it++){
 90             I6+=e[*it].l;
 91         }
 92         if(I5<=q5 && I6<=q6){
 93             printf("%d\n",I5*p5+I6*p6);
 94             for(set<int>::iterator it = e5.begin();it!=e5.end();it++){
 95                 printf("%d %d\n",e[*it].id,i5);
 96             }
 97             for(set<int>::iterator it = e6.begin();it!=e6.end();it++){
 98                 printf("%d %d\n",e[*it].id,i6);
 99             }
100         }else{
101             printf("Impossible\n");
102         }
103     }
104     return 0;
105 }

/*3538*/

转载于:https://www.cnblogs.com/sineatos/p/3933886.html

POJ - 3538 - Domestic Networks相关推荐

  1. POJ 3538 Domestic Networks(DP)

    题目链接:点击打开链接 思路: 选一些边, 使得任意两点都可以相互到达且花费最小,  这显然是最小生成树, 将边挑选出来之后, 如果贪心选取的话, 有可能导致无解, 所以我们考虑用动态规划. 根据数据 ...

  2. POJ3538 Domestic Networks DP+MST

    简单DP DP之前先求一个最小生成树 状态转移 f[i][j]  表示前i条边用了j单位的5号材料所用的最小花费 f[i][j]=min(f[i][j],f[i-1][j-len]+len*c5) f ...

  3. POJ 3538/Codeforces 100078D:Domestic Networks 解题报告

    点击这里进入题目 题意:有N个顶点M条边,每一条边都有相应的长度,你有长度为5和长度为6的线,你要用它来覆盖这些边,使花费最小. 思路:一个图上要求最小的花费,很明显要先处理最小生成树 AC程序 // ...

  4. POJ 2799 IP Networks

    network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...

  5. 《题目与解读》红书 训练笔记目录《ACM国际大学生程序设计竞赛题目与解读》

    虽然2012年出版的老书了,但是是由三次世界冠军的上海交大ACM队出版的书籍,选择的题目是ACM经典中的经典,书中有非常详细的题解,可以学到很多东西,值得一刷. 目录 第一部分 第一章 数学 1.1 ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. 图融合GCN(Graph Convolutional Networks)

    图融合GCN(Graph Convolutional Networks) 数据其实是图(graph),图在生活中无处不在,如社交网络,知识图谱,蛋白质结构等.本文介绍GNN(Graph Neural ...

  8. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  9. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

最新文章

  1. iOS iTunes Connect协议更新导致无法构建新版本
  2. 4位数学家获得2018年菲尔兹奖
  3. PHP配置问题:AppServ安装discuz出错 Fatal error:
  4. js排序的时间复杂度_经典排序方法的python实现和复杂度分析
  5. 服务器是计算机的一种 是指,pc服务器是指什么意思
  6. 稳定性保障6步走:高可用系统大促作战指南!
  7. 正则表达式发明者_浅谈正则表达式背后的基本原理
  8. su 切换,提示:“密码不正确”;
  9. 基于GDAL的一个通用的3×3模板函数
  10. diy的电流电压表,高频率采集,上位机同步显示
  11. 关于瑞昱8763bfr的学习总结(1)
  12. 做一晚黄牛能坑多少昧心钱?
  13. 兑吧开发规范《源Java手册》
  14. 更改Ubuntu桌面环境
  15. 谁将成为中国版底特律?
  16. 设计水杯,门锁的测试用例
  17. 在EPICS定义一个新的记录类型
  18. 新旧电脑安装win11系统【超简单教程】
  19. 嵌入式开发-服务器(一) MQTT服务器
  20. 【苹果相册推送位置推送iMessage】软件安装TestFlight计划的信息

热门文章

  1. 终端bash美化(FC)
  2. Ansible03-管理变量、加密、事实
  3. Observables简介以及它们与Promise有何不同
  4. 把canvas放在盒子内_如何将您的专业知识放在盒子中并出售
  5. 对分组交换(packet switching)高效迅速灵活可靠四个优点的理解
  6. Python 用户的三次登录机会
  7. 宁波大红鹰学院计算机科学与技术,2019宁波大红鹰学院专业排名
  8. java线程安全的set_Java并发编程之set集合的线程安全类你知道吗
  9. puppet yum模块、配置仓储、mount模块
  10. 3.Java集合-HashSet实现原理及源码分析