先上题目:

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",否则就有可能存在合法的方案,这里我们可以用背包选电线。我们的目标是让尽量多的电线使用便宜的那一种电线,所以我们对便宜的那种电线进行一次背包,剩下的线路用贵的电线,然后判断一下是否符合输出的电线数量,如果符合就输出所有结果,否者就是非法的。
 
上代码:
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#define MAX 10002
using namespace std; typedef struct edge{
int u,v,l,id; bool operator < (const edge& o)const{
return l<o.l;
}
}edge; edge e[MAX];
int p[MAX],mst[MAX],tot;
int n,m,p5,q5,p6,q6,i5,i6;
int pag[MAX];
bool f[MAX];
set<int> e5,e6; int findset(int u){
return u==p[u] ? p[u] : p[u]=findset(p[u]);
} void MST(){
tot=;
int u,v;
for(int i=;i<m;i++){
u=findset(e[i].u); v=findset(e[i].v);
if(p[u]!=p[v]){
p[v]=u;
mst[tot++]=i;
}
}
} int main()
{
int I5,I6;
//freopen("data.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF){
for(int i=;i<=n;i++) p[i]=i;
for(int i=;i<m;i++){
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].l);
e[i].id=i+;
}
scanf("%d %d %d %d",&p5,&q5,&p6,&q6);
i5=; i6=;
if(p5>p6){
swap(p5,p6);
swap(q5,q6);
swap(i5,i6);
}
sort(e,e+m);
MST();
if(tot!=n-){
printf("Impossible\n");
continue;
} /*********pag*********/
memset(f,,sizeof(f));
e5.clear(); e6.clear();
//memset(pag,0,sizeof(pag));
f[]=;
for(int i=;i<tot;i++){
int l=e[mst[i]].l;
e6.insert(mst[i]);
for(int j=q5;j>=l;j--){
if(!f[j] && f[j-l]){
f[j]=;
pag[j]=mst[i];
}
}
}
int k=q5;
while(!f[k]) k--;
while(k>){
e5.insert(pag[k]);
e6.erase(pag[k]);
k-=e[pag[k]].l;
}
I5=I6=;
for(set<int>::iterator it = e5.begin();it!=e5.end();it++){
I5+=e[*it].l;
}
for(set<int>::iterator it = e6.begin();it!=e6.end();it++){
I6+=e[*it].l;
}
if(I5<=q5 && I6<=q6){
printf("%d\n",I5*p5+I6*p6);
for(set<int>::iterator it = e5.begin();it!=e5.end();it++){
printf("%d %d\n",e[*it].id,i5);
}
for(set<int>::iterator it = e6.begin();it!=e6.end();it++){
printf("%d %d\n",e[*it].id,i6);
}
}else{
printf("Impossible\n");
}
}
return ;
}

/*3538*/

POJ - 3538 - Domestic Networks的更多相关文章

  1. 100078D Domestic Networks

    传送门 题目大意 有两种染料,给定它们的单价和数量,每染色一米需耗费一个单位的染料,一条边只能用一种燃料,给你一张图,要求你将其中的一些边染色使得在满足图联通的情况下花费最小并输出方案. 分析 首先, ...

  2. POJ 2799 IP Networks

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

  3. [poj] 1236 networks of schools

    原题 这是一道强连通分量板子题. 显然subtask1 是要输出入度为0的点的个数 而subtask2,我们考虑一下最优一定是把一个出度为零的点连到入度为零的点上,这样我们要输出的就是max(出度为零 ...

  4. POJ 1236 Networks of School Tarjan 基础

    题目大意: 给一个有向图,一个文件可以从某个点出发传递向他能连的边 现在有两个问题 1.至少需要多少个放文件可以让整个图都有文件 2.可以进行一个操作:给一对点(u,v)连一条u->v的有向边, ...

  5. POJ - 1978 Hanafuda Shuffle

    最初给牌编号时,编号的顺序是从下到上:洗牌时,认牌的顺序是从上到下.注意使用循环是尽量统一“i”的初始化值,都为“0”或者都为“1”,限界条件统一使用“<”或者“<=”. POJ - 19 ...

  6. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  7. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

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

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

  9. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

随机推荐

  1. [App Store Connect帮助]一、 App Store Connect 使用入门(1)App Store Connect 工作流程

    您使用 App Store Connect 提交并管理您在 App Store 中销售的 App,使用 TestFlight 分发您 App 的 Beta 版本,接受法律协议,输入您的税务和银行业务信 ...

  2. [Swift通天遁地]二、表格表单-(11)创建星期选项表单和拥有浮动标签的文本框

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. [Swift通天遁地]八、媒体与动画-(14)使用TextKit快速实现文章的分栏效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. Akka源码分析-Remote-Creating Actors Remotely

    在akka官网中关于远程actor交互,介绍了两种方法,一种是通过actorSelection查询,另一种是通过actorOf在远程节点创建一个actor.actorSelection我们之前的博客中 ...

  5. PHP入门开发

    1.下载开发工具phpstorm 地址:http://www.jetbrains.com/phpstorm/download/download-thanks.html?platform=windows ...

  6. ACM_统计字符串

    统计字符串 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定n个字符串,统计字符串的个数. 如给定 5 sss ab sss ...

  7. Flume NG基本架构与Flume NG核心概念

    导读 Flume NG是一个分布式.可靠.可用的系统,它能够将不同数据源的海量日志数据进行高效收集.聚合.移动,最后存储到一个中心化数据存储系统中. 由原来的Flume OG到现在的Flume NG, ...

  8. debounce还是throttle(去抖和节流)

    debounce 去抖 我的理解很简单,比方说window.onscroll会疯狂触发handler,此时给它一个debounce(handler, delayTime). 就是不管你延时时间内触发了 ...

  9. CSS知识点整理(2):框模型,定位

    1. 框模型:Box Model 规定了元素处理元素框处理元素内容.外边距.边框.内边距的方式. 2. 当边距给定的值 可以小于4个.CSS定义了一些规则.处理这中情况: 如果缺少左外边距的值,则使用 ...

  10. JS高级——Function原型链

    基本概念 1.函数可以通过Function new出来,那么Function可以被称作构造函数,被new出来的函数可以被称为一个对象 2.Function既然是构造函数,那么肯定也有原型,它的原型是一 ...