Poj-3538-domestic Networks

Source: Internet
Author: User
Tags integer numbers

First on the topic:

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. He network connects apartments and spans over multiple buildings.

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

domestic Networks Inc. Buys cable in the nearest cable. Unfortunately, shop sells only Category 5 and 6 cables at the price of p5 and p6 rubles per meter respective Ly. Moreover, there is only Q5 meters of Category 5 cable and Q6 meters of Category 6 cable available in th E 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 being made and cable category for each link should is a single piece of cable of either 5 or 6 category). The cost of the plan was the sum of the 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-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 The can is connected by the Link and li -link length in meters (0≤ li ≤100). Apartments is numbered from 1 to N.

The last line of the input file contains four integer numbers: p5, Q5, P6 and Q6-pri Ce and quantity of Category 5 and 6 cables respectively (1≤ pi, qi ≤10 000).

Output

If All apartments can is connected with the available cable, output n lines-an Optimal network construction PLA N. The first line of the plan must contain plan ' s cost.  Other lines of the plan must consist of both integers numbers each: ai -number of the link to make and ci -the category of the cable to make it. Links is numbered from 1 to m in the order they is specified in the input file. If there is more than one optimal plans, and output any of them.

If There is no plan meeting all requirements, the 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, Northern subregion test instructions: give you a picture, each side has a length, give you a certain number of two kinds of wires, tell you their number and unit price, ask you can use these wires to spend the least cost to make each point on the diagram connected.  If you can minimize the cost of the output and output which lines are selected, which wire they use, which can only be used on a single line of wire. This is done by first seeking the MST, if there is no MST, the output "impossbile", otherwise there may be a legitimate scheme, here we can use the backpack to select the wire. Our goal is to make as many wires as possible to use the cheap one, so we carry on a backpack for the cheap kind of wire, the rest of the line with expensive wire, and then determine whether the output of the number of wires, if it is consistent with the output of all results, whether the person is illegal. On the code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <Set>5 #defineMAX 100026 using namespacestd;7 8typedefstructedge{9     intU,v,l,id;Ten  One     BOOL operator< (Constedge& o)Const{ A         returnl<O.L; -     } - }edge; the  - Edge E[max]; - intP[max],mst[max],tot; - intN,m,p5,q5,p6,q6,i5,i6; + intPag[max]; - BOOLF[max]; + Set<int>E5,e6; A  at intFindset (intu) { -     returnU==p[u]? P[u]: p[u]=Findset (P[u]); - } -  - voidMST () { -tot=0; in     intu,v; -      for(intI=0; i<m;i++){ toU=findset (E[I].U); v=Findset (E[I].V); +         if(p[u]!=P[v]) { -p[v]=u; themst[tot++]=i; *         } $     }Panax Notoginseng } -  the intMain () + { A     intI5,i6; the     //freopen ("Data.txt", "R", stdin); +      while(SCANF ("%d%d", &n,&m)! =EOF) { -          for(intI=1; i<=n;i++) p[i]=i; $          for(intI=0; i<m;i++){ $scanf" %d%d%d",&e[i].u,&e[i].v,&e[i].l); -e[i].id=i+1; -         } thescanf"%d %d%d%d",&p5,&q5,&p6,&Q6); -i5=5; i6=6;Wuyi         if(p5>P6) { the swap (P5,P6); - swap (Q5,Q6); Wu swap (I5,I6); -         } AboutSort (e,e+m); $ MST (); -         if(tot!=n-1){ -printf"impossible\n"); -             Continue; A         } +  the         /*********pag*********/ -Memset (F,0,sizeof(f)); $ e5.clear (); E6.clear (); the         //memset (pag,0,sizeof (PAG)); thef[0]=1; the          for(intI=0; i<tot;i++){ the             intL=E[MST[I]].L; - E6.insert (Mst[i]); in              for(intj=q5;j>=l;j--){ the                 if(!f[j] && f[j-L]) { thef[j]=1; Aboutpag[j]=Mst[i]; the                 } the             } the         } +         intk=Q5; -          while(!f[k]) k--; the          while(k>0){Bayi E5.insert (Pag[k]); the e6.erase (Pag[k]); thek-=E[PAG[K]].L; -         } -i5=i6=0; the          for(Set<int>::iterator it = E5.begin (); It!=e5.end (); it++){ thei5+=e[*IT].L; the         } the          for(Set<int>::iterator it = E6.begin (); It!=e6.end (); it++){ -i6+=e[*IT].L; the         } the         if(I5<=q5 && i6<=Q6) { theprintf"%d\n", i5*p5+i6*P6);94              for(Set<int>::iterator it = E5.begin (); It!=e5.end (); it++){ theprintf"%d%d\n", e[*it].id,i5); the             } the              for(Set<int>::iterator it = E6.begin (); It!=e6.end (); it++){98printf"%d%d\n", e[*it].id,i6); About             } -}Else{101printf"impossible\n");102         }103     }104     return 0; the}
/*3538*/

Poj-3538-domestic Networks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.