24 questions about the space flight plan of the network flow (nefu476)

Source: Internet
Author: User
Tags prepare

Description

Professor W is planning a series of space flights for the National Space Center. Every space flight can make a series of commercial experiments to gain profits. A selection of experimental set E={e1,e2,...,em} has been identified, and the set of all instruments required for these experiments to be used I={i1,i2, ... in}. The instrument that the experimental EJ needs to use is a subset of I. The cost of configuring the instrument IK is ck USD. The sponsors of the experiment EJ have agreed to pay PJ dollars for the results of the experiment. Prof W's task is to find an effective algorithm to determine which experiments are to be carried out in a space flight and which instruments will be used to make space flight the largest net yield. The net income here is the difference between the full amount of revenue obtained from the experiment and the total cost of the equipment being configured.

Input

Multiple sets of data input.
The 1th row of each group of inputs has 2 positive integers m and N. M is the number of experiments, and N is the number of instruments. The next M-line, each row is an experimental data. The first number of sponsors agreed to pay the cost of the experiment, followed by the number of instruments required for the experiment, followed by the number of the corresponding instrument. The number of n in the last line is the cost of configuring each instrument.

Output

NET yield for each set of output best protocols

Sample Input

2 3
10 2 1 2
25 2 2 3
5 6 7

Sample Output

17
Topic from nefu476
Train of thought: This topic is the biggest right closed graph, the profit is the experiment funds-the weight of the maximum right closed graph (minimum cut)
The source point even the experiment, the weight value is the fund, the experiment even corresponding instrument weight value is infinite, the instrument connects the meeting point, the weight is the instrument cost, thus can build the diagram

#include <stdio.h> #include <iostream> #include <string.h> using namespace std;
const int OO=1E9;
/**oo represents infinity */const int mm=99999999;
/**MM represents the maximum number of edges, remember that if the original twice times, in addition to the side of the time are two-way * * const int mn=999;
    /**MN represents the maximum number of points */struct SA {int val;
int num;

}YIQI[MN];
    struct SA2 {int val;
int NEED[MN];
}SHIYAN[MN];
int Node,src,dest,edge;
/**node indicates the number of nodes, SRC represents the source point, dest represents the meeting point, EDGE statistics edge number */int ver[mm],flow[mm],next[mm];
The node that the/**ver edge points to, the capacity of the flow edge, next edge of the next list */int head[mn],work[mn],dis[mn],q[mn];
    void Prepare (int _node, int _src,int _dest) {node=_node,src=_src,dest=_dest;
    for (int i=0; i<node; ++i) head[i]=-1;
edge=0; }/** adds a U to v capacity of C */void Addedge (int u, int v, int c) {ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=
    edge++;
ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
    }/** calculates the shortest distance between each point and the source point, if it cannot reach the end of the meeting point description algorithm */bool Dinic_bfs () {int i,u,v,l,r=0;
    for (i=0; i<node; ++i) dis[i]=-1;
    dis[q[r++]=src]=0; for (l=0; L&LT;r;
                ++l) for (I=head[u=q[l]]; i>=0; i=next[i]) if (flow[i]&&dis[v=ver[i]]<0) {
                /** This side must have the remaining capacity */dis[q[r++]=v]=dis[u]+1;
            if (v==dest) return 1;
} return 0;
    /** search for an augmented path algorithm for a viable stream, find it by node distance, speed up */int Dinic_dfs (int u, int exp) {if (u==dest) return exp; /**work is a temporary chain header, where I refer to it, so that the searched edges no longer look for */for (int &i=work[u],v,tmp; i>=0; i=next[i]) if (flow[i]&&amp
            ;d is[v=ver[i]]==dis[u]+1&& (Tmp=dinic_dfs (V,min (exp,flow[i))) >0) {flow[i]-=tmp;
            flow[i^1]+=tmp;
        /** positive and negative side capacity change */return TMP;
} return 0;
    } int Dicnic_flow () {int i,ret=0,delta;
        while (DINIC_BFS ()) {for (i=0; i<node; ++i) work[i]=head[i];
           while (Delta=dinic_dfs (Src,oo)) {Ret+=delta;
        cout<<ret<<endl;
}} return ret; } int main () {int m,n,x;
    Ios::sync_with_stdio (FALSE);
        while (cin>>m>>n) {memset (shiyan,0,sizeof (Shiyan));
        memset (yiqi,0,sizeof (Yiqi));
        int ans=0;
        Prepare (n+m+2,0,n+m+1);
            for (int i=1;i<=m;i++) {cin>>shiyan[i].val;
            Ans+=shiyan[i].val;
            cin>>yiqi[i].num;
            for (int j=1;j<=yiqi[i].num;j++) {cin>>shiyan[i].need[j];
        }} for (int i=1;i<=n;i++) {cin>>yiqi[i].val;
             } for (int i=1;i<=m;i++) {Addedge (0,i,shiyan[i].val);
             for (int j=1;j<=yiqi[i].num;j++) {Addedge (I,shiyan[i].need[j]+m,oo);
        }} int flag[mn];
        memset (flag,0,sizeof (flag)); for (int i=1;i<=n;i++) {/* for (int j=1;j<=yiqi[i].num;j++)//has been wrong in this piece, the writing is too troublesome, the wrong can not find, do not need to traverse and then see if the instrument Appear and then add edge, direct the instrument to the meeting pointJust connect it. {if (!flag[shiyan[i].need[j]]) {Addedge

                    (Shiyan[i].need[j]+m,n+m+1,yiqi[shiyan[i].need[j]].val);
                   Flag[shiyan[i].need[j]]=1;
        }}*/Addedge (I+m,n+m+1,yiqi[i].val);
        } int Sum=ans-dicnic_flow ();
    cout<<sum<<endl;
} return 0;
 }

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.