POJ - 3538 - Domestic Networks

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   使用   io   strong   

先上題目:

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 71 2 72 6 51 4 82 3 53 4 55 6 63 5 32 11 3 100

Sample Output

651 52 64 65 67 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*/

 

POJ - 3538 - Domestic Networks

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.