先上题目:

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. 查看mysql是否安装成功和mysql的版本信息

    转自:https://blog.csdn.net/hellocsz/article/details/81241204 使用快捷键win+R打开 进入mysql的安装目录下的\bin(本人安装路劲为E: ...

  2. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  3. El Dorado(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=2372 题意:给出n个数,求长度为m的递增子序列的数目. 思路:状态转移方程 dp[i][j] = sum(dp[ ...

  4. AirtestIDE详解(跨平台的UI自动化编辑器)

    Airtest 是网易出品的一款基于图像识别和poco控件识别的一款UI自动化测试工具. AirtestIDE 是一个跨平台.多端(Windows.web.android.ios.游戏)的UI自动化测 ...

  5. composer 下载安装

    linux/mac os curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/compos ...

  6. Java的Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区别???

    最近在看Java多线程这本书,但是发现里面有个概念自己搞不清楚.就是Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区 ...

  7. NHibernate系列学习(一)-看看用NH怎么做增速改查

    1.本次所有代码是在一下环境下运行的 学习系统:win8 64bit 开发环境:VS2013,MSSQL2012 NHibernate版本:NHibernate-4.0.3.GA [文章结尾有本次笔记 ...

  8. Java学习笔记_网络(2)

    单向的客户端发送服务器端接收并显示,没有使用多线程 import javax.swing.*; import java.awt.*; import java.io.*; import java.net ...

  9. Java—解压zip文件

    import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import ja ...

  10. BFS小结

    其实bfs本身不难,甚至不需要去学习,只要知道它的特性就可以写出来了.往往,bfs都是用递归做的.递归比循环更容易timeout.所以这次遇到一题bfs,卡时间的就悲剧了. PAT1076 #incl ...