POJ - 3538 - 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: p5, q5, p6 and q6 — price and quantity of category 5 and 6 cables respectively (1 ≤ pi, qi ≤ 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
#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的更多相关文章
- 100078D Domestic Networks
传送门 题目大意 有两种染料,给定它们的单价和数量,每染色一米需耗费一个单位的染料,一条边只能用一种燃料,给你一张图,要求你将其中的一些边染色使得在满足图联通的情况下花费最小并输出方案. 分析 首先, ...
- POJ 2799 IP Networks
network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...
- [poj] 1236 networks of schools
原题 这是一道强连通分量板子题. 显然subtask1 是要输出入度为0的点的个数 而subtask2,我们考虑一下最优一定是把一个出度为零的点连到入度为零的点上,这样我们要输出的就是max(出度为零 ...
- POJ 1236 Networks of School Tarjan 基础
题目大意: 给一个有向图,一个文件可以从某个点出发传递向他能连的边 现在有两个问题 1.至少需要多少个放文件可以让整个图都有文件 2.可以进行一个操作:给一对点(u,v)连一条u->v的有向边, ...
- POJ - 1978 Hanafuda Shuffle
最初给牌编号时,编号的顺序是从下到上:洗牌时,认牌的顺序是从上到下.注意使用循环是尽量统一“i”的初始化值,都为“0”或者都为“1”,限界条件统一使用“<”或者“<=”. POJ - 19 ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- Poj(2784),二进制枚举最小生成树
题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
随机推荐
- Python的包管理工具easy_install, setuptools, pip,distribute介绍
1.相互之间的关联 easy_install, setuptools, pip,distribute,这几个工具的联系,如下图: 可以看到distribute是setuptools的取代,pip是ea ...
- bzoj1992鬼谷子的钱袋(二分乱搞 二进制)
1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3223 Solved: 2333 Descriptio ...
- bzoj题目分类
转载于http://blog.csdn.net/creationaugust/article/details/513876231000:A+B 1001:平面图最小割,转对偶图最短路 1002:矩阵树 ...
- vscode常用的快捷键
对于编程人员来说,记住一些常用的快捷键能够提高工作效率:我认为,对于编程人员来说,掌握一些常用的快捷键是非常有必要的! Ctrl + Shift + N 打开新的编辑器窗口 Ctrl + Shift ...
- Educational Codeforces Round 45
A. 一个小模拟 不解释 //By SiriusRen #include <bits/stdc++.h> using namespace std; long long n,m,a,b ...
- 318 Maximum Product of Word Lengths 最大单词长度乘积
给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
- ssh项目导入报the import javax.servlet cannot be resolved
在做javaWeb项目时,我们经常会出现丢失包的情况,如下图所示的错误,我们应该怎么解决呢? 根据网上教程向工程中加入tomcat的servlet-api.jar和jsp-api.jar的包 此时项目 ...
- Java中常用的操作PDF的类库
iText iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好的给合.使用 ...
- Android 8.0 启动后台service 出错 IllegalStateException: Not allowed to start service Intent
错误原因: Android 8.0 不再允许后台service直接通过startService方式去启动, 具体行为变更如下: 如果针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况 ...
- UVM基础之--------uvm_root
uvm_root 是uvm的顶层实例扮演了一个top-level and phase controller 的作用,对于component来说.该类不需要用户实例化,他是一个自动实例化了的类,用户直接 ...