Question: Linear Planning and network flow 24 question T2 space flight plan

Source: Internet
Author: User

Space flight plan problems

Problem description

Professor W is planning a series of space flights for the National Space Center. Each space flight can carry out a series of commercial experiments to make profits. An available experiment set E = {E1, E2 ,..., Em}, and the set of all the instruments required to perform these experiments I = {I1, I2 ,... In }. The instrument used in the experiment J is a subset of I rj in I. The cost of configuring the instrument IK is USD ck. The sponsor of the experiment J has agreed to pay pj usd for the results. Professor W's task is to find an effective algorithm to determine which experiments are to be performed during a space flight and to configure which instruments to maximize the net income of space flight. The net income here refers to the difference between the total income obtained from the experiment and the total cost of the instrument configuration.

Programming task

Program the given experiment and instrument configurations to find the test plan with the largest net income.

Data Input

Input data is provided by the file input.txt. There are two positive integers m and n in row 1st of the file. M indicates the number of labs, and N indicates the number of instruments. The next m rows are related to an experiment. The first number of sponsors agreed to pay for the experiment, followed by the numbers of several instruments required for the experiment. The N number in the last line is the cost of configuring each instrument.

Result output

When the program runs the program, the experimental program is output to the output.txt file. The first row is the lab number, the second row is the instrument number, and the last row is the net income.

Input File example: input.txt

2 3

10 1 2

25 2 3

5 6 7

Output file example output.txt

1 2

1 2 3

17

Analysis:

We can use the largest weighted closed graph model to create a graph. we can regard the experiment as the X set of the bipartite graph, and the instrument as the y set of the Bipartite Graph. Add S, add T, and s to connect the edge to all x sets, and the edge weight is the income of the experiment. All instruments connect to the T side, and the edge weight is the cost of changing the instrument. Edge connecting to the instrument on which the experiment depends. The Edge Weight iS INF.

Then the minimum cut is obtained. The answer is the sum of all lab income, total-maxflow.

... Let's take a look at why we can create a graph.

Define the s set divided by a cut as a solution, and the cut set capacity is equal to the income of unselected experiments + the cost of the selected instrument. In this way, total-maxflow indicates the selected experiment revenue-the cost of the selected instrument.

In this way, the problem is transformed into "Total-maxflow", that is, to minimize maxflow.

So ...... You know .........

Code:

#include <cstdio>#include <cstring>using namespace std;const int inf = 1 << 30;const int maxn = 1010;const int maxm = 2020;inline int min(int a,int b) {return a < b ? a : b;}int tot;struct Edge{    int v,w,nxt;}e[maxm];int head[maxn];int d[maxn],q[maxn];int sta,end;inline void addedge(int u,int v,int d){    e[tot].v = v,e[tot].w = d,e[tot].nxt = head[u],head[u] = tot ++;    e[tot].v = u,e[tot].w = 0,e[tot].nxt = head[v],head[v] = tot ++;}inline int bfs(){    memset(d,-1,sizeof(d));    int r = d[q[0] = sta] = 0;    for(int l = 0;l <= r;l ++)        for(int k = head[q[l]]; ~k ;k = e[k].nxt)            if(e[k].w && !~d[e[k].v])                d[q[++ r] = e[k].v] = d[q[l]] + 1;    return ~d[end];}int find(int x,int low){    if(x == end)    return low;    int a;    for(int k = head[x]; ~k ;k = e[k].nxt)        if(e[k].w && d[e[k].v] == d[x] + 1 && (a = find(e[k].v,min(low,e[k].w))))        {            e[k].w      -= a;            e[k ^ 1].w  += a;            return a;        }    return 0;}int dinic(){    int ret = 0,t;    while(bfs())        while(t = find(sta,inf))   ret += t;    return ret;}bool bLineBreak;void scan(int & n){    char c; n = 0; bLineBreak = 0;    while(c = getchar(),c < ‘0‘ || c > ‘9‘) if(c == ‘\n‘) bLineBreak = 1;    n = c - ‘0‘;    while(c = getchar(),(c >= ‘0‘ && c <= ‘9‘)) n = n * 10 + c - ‘0‘;    if(c == ‘\n‘)   bLineBreak = 1;}inline void debug_dinic(){    int n,m;    scanf("%d%d",&n,&m);    sta = 0,end =1;    memset(head,-1,sizeof(head));    while(m --)    {        int u,v,w;        scanf("%d%d%d",&u,&v,&w);        addedge(u,v,w);    }    printf("  %d \n",dinic());}inline void debug_scan(){    int n;    do    {        scan(n);        printf("%d\n",n);    } while(!bLineBreak);}int experiment[maxn],vis[maxn];int main(){    int m,n,sum = 0;    sta = 0,end = 1;    scanf("%d%d",&m,&n);    memset(head,-1,sizeof(head));    for(int i = 0;i < m;i ++)    {        int val,id;        scanf("%d",&val);        addedge(sta,i + 2,val),sum += val;        do        {            scan(id);            addedge(i + 2,m + 1 + id,inf);        } while(!bLineBreak);    }    for(int i = 0;i < n;i ++)    {        int cost;        scanf("%d",&cost);        addedge(m + 2 + i,end,cost);    }    int ans = sum - dinic();    int cnt = 0;    for(int k = head[0]; ~k ;k = e[k].nxt)  if(e[k].w)    {        int id = e[k].v;        experiment[cnt ++] = id;        printf("%d ",id - 1);    }    putchar(‘\n‘);    for(int i = 0;i < cnt;i ++)    {        for(int k = head[experiment[i]]; ~k ;k = e[k].nxt)        {            int v = e[k].v;            if(vis[v]) continue;            vis[v] = 1;            if(!e[head[v]].w)    printf("%d ",e[k].v - 1 - m);        }    }    printf("\n%d",ans);    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.