Poj3538 domestic networks DP + MST

Source: Internet
Author: User
Tags integer numbers

Simple DP

Calculate a minimum spanning tree before DP

Status Transfer

F [I] [J] indicates the minimum cost for the first I edge to use the No. 5 material in Unit J.

F [I] [J] = min (F [I] [J], F [I-1] [J-Len] + Len * C5)

F [I] [J] = min (F [I] [J], F [I-1] [J] + Len * C6)

Then you can trace the output.

The memory is a little tight. Use a rolling array.

Domestic Networks
Time limit:2000 ms   Memory limit:65536 K
Total submissions:625   Accepted:174   Special Judge

Description

Alex is a system administratorDomestic 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 priceP5 andP6 rubles per meter respectively. Moreover, there are onlyQ5 meters
Of Category 5 cable andQ6 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 shocould 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 shocould 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 andM-The number of possible links (1 ≤N≤ 1000, 1 ≤M≤ 10 000 ).

FollowingMLines contain possible link descriptions. Each description consists of three integer numbers:AIAndBi-Apartments that can be connected by the link andLi-Link length in
Meters (0 ≤Li≤ 100). apartments are numbered from 1N.

The last line of the input file contains four integer numbers:P5,Q5,P6 andQ6-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, outputNLines-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 andCi-The category of the cable to make it of. links are numbered from 1MIn 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
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;#define INF 0x3f3f3f3f#define MAXN 1010struct edge{    int id;int s,e;int dis;};struct sel{    int id;    int dis;}l[MAXN];edge e[12000];int  set[MAXN];int n,m;int p1,p2,q1,q2;int top;bool cmp(edge a,edge b){return a.dis<b.dis;}void init(int n){for(int i=0;i<=n;i++) set[i]=i;}int find(int a){int root=a;int temp;while(set[root]!=root)root=set[root];while(set[a]!=root){temp=a;a=set[a];set[temp]=root;}return root;}bool merge(int a,int b){int x=find(a);int y=find(b);if(x==y)return false;set[x]=y;return true;}int kruscal(){int num=0,sum=0;top=0;for(int i=0;i<m && num<n-1;i++){if(merge(e[i].s,e[i].e)){sum+=e[i].dis;l[top].id=e[i].id;l[top++].dis=e[i].dis;num++;}}if(num!=n-1) return -1;return sum;}long long dp[2][10105];int path[MAXN][10105];void dfs(int cnt,int y){    if(!cnt) return;    if(path[cnt][y]!=y)    {        dfs(cnt-1,path[cnt][y]);        printf("%d 5\n",l[cnt-1].id);    }    else    {        dfs(cnt-1,y);        printf("%d 6\n",l[cnt-1].id);    }}bool cmp2(sel a,sel b){    return a.id<b.id;}int main(){    while(~scanf("%d%d",&n,&m))    {        init(n);        int dis;        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&e[i].s,&e[i].e,&e[i].dis);            e[i].id=i+1;        }        sort(e,e+m,cmp);        scanf("%d%d%d%d",&p1,&q1,&p2,&q2);        if((dis=kruscal())==-1 ||dis>q1+q2)            printf("Impossible\n");        else        {            int total=0;            sort(l,l+top,cmp2);            for(int i=0;i<=q1;i++)                dp[0][i]=dp[1][i]=INF;            dp[0][0]=0;            int cur=0;            for(int i=1;i<=top;i++)            {                total+=l[i-1].dis;                int c=l[i-1].dis;                for(int j=q1;(j>=c||total-j<=q2)&&j>=0;j--)                {                    if(total-j<=q2&&dp[cur][j]!=INF&&dp[cur][j]+c*p2<dp[cur^1][j])                    {                        dp[cur^1][j]=dp[cur][j]+c*p2;                        path[i][j]=j;                    }                    if(j>=c&&dp[cur][j-c]!=INF&&dp[cur][j-c]+c*p1<dp[cur^1][j])                    {                        dp[cur^1][j]=dp[cur][j-c]+c*p1;                        path[i][j]=j-c;                    }                }                for(int j=q1;j>=0;j--) dp[cur][j]=INF;                cur=cur^1;            }            long long ans=INF,y=-1;            for(int i=0;i<=q1;i++)            {                if(ans>dp[cur][i])                {                    ans=dp[cur][i];                    y=i;                }            }            if(ans==INF)                printf("Impossible\n");            else{                printf("%lld\n",ans);                dfs(top,y);            }        }    }}

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.