Nyist 531 space flight plan

Source: Internet
Author: User
Space flight schedule time limit: 1000 MS | memory limit: 65535 kb difficulty: 4
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 the subset RJ of 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. Program the given experiment and instrument configurations to find the test plan with the largest net income.
 
Input
Multiple groups of test data (no more than 500 groups)
There are two positive integers m and n (m, n <= 1st) in each group of data ). 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 F (F <10000), followed by the number of instruments required for the experiment T, followed by the number of T instruments. The number of N in the last line is the cost Pi (pI <= 100) for each instrument ).
Output
Each group of data outputs occupies one row, and the maximum net income of the output is obtained (if no benefit is available, 0 is output ).
Sample Input
2 310 2 1 225 2 2 35 6 7
Sample output
17
Uploaded
ACM _ Yang yanxi
Problem-solving: the largest weighted closed subgraph, the input of the NIMA egg hurts. Well, it seems that there are several groups of data in the original question. Nanyang only needs to output the maximum weight and does not need to output the experiment number and Instrument Number.
  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cmath>  5 #include <algorithm>  6 #include <climits>  7 #include <vector>  8 #include <queue>  9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 500; 18 struct arc{ 19     int to,flow,next; 20     arc(int x = 0,int y = 0,int z = -1){ 21         to = x; 22         flow = y; 23         next = z; 24     } 25 }; 26 arc e[maxn*maxn]; 27 int head[maxn],d[maxn],cur[maxn]; 28 int tot,m,n,S,T; 29 void add(int u,int v,int flow){ 30     e[tot] = arc(v,flow,head[u]); 31     head[u] = tot++; 32     e[tot] = arc(u,0,head[v]); 33     head[v] = tot++; 34 } 35 bool bfs(){ 36     memset(d,-1,sizeof(d)); 37     d[S] = 1; 38     queue<int>q; 39     q.push(S); 40     while(!q.empty()){ 41         int u = q.front(); 42         q.pop(); 43         for(int i = head[u]; ~i; i = e[i].next){ 44             if(e[i].flow && d[e[i].to] == -1){ 45                 d[e[i].to] = d[u] + 1; 46                 q.push(e[i].to); 47             } 48         } 49     } 50     return d[T] > -1; 51 } 52 int dfs(int u,int low){ 53     if(u == T) return low; 54     int tmp = 0,a; 55     for(int &i = cur[u]; ~i; i = e[i].next){ 56         if(e[i].flow && d[e[i].to] == d[u] + 1 &&(a=dfs(e[i].to,min(e[i].flow,low)))){ 57             e[i].flow -= a; 58             e[i^1].flow += a; 59             tmp += a; 60             low -= a; 61             if(!low) break; 62         } 63     } 64     if(!tmp) d[u] = -1; 65     return tmp; 66 } 67 int dinic(){ 68     int ans = 0; 69     while(bfs()){ 70         memcpy(cur,head,sizeof(head)); 71         ans += dfs(S,INF); 72     } 73     return ans; 74 } 75 void go(int u){ 76     char str[maxn]; 77     gets(str); 78     for(int i = 0; str[i];){ 79         while(str[i] &&(str[i] < ‘0‘ || str[i] > ‘9‘)) ++i; 80         int x = 0; 81         while(str[i] && str[i] >= ‘0‘ && str[i] <= ‘9‘){ 82             x = x*10 + str[i++] - ‘0‘; 83         } 84         if(x) add(u,x+m,INF); 85     } 86 } 87 bool vis[maxn]; 88 vector<int>ans1,ans2; 89 void dfs(int u){ 90     vis[u] = true; 91     for(int i = head[u]; ~i; i = e[i].next){ 92         if(vis[e[i].to] || e[i].flow == 0) continue; 93         if(e[i].to > m) ans2.push_back(e[i].to-m); 94         else ans1.push_back(e[i].to); 95         dfs(e[i].to); 96     } 97 } 98 bool cmp(int a,int b){ 99     return a > b;100 }101 int main() {102     int w;103     while(~scanf("%d %d",&m,&n)){104         memset(head,-1,sizeof(head));105         int ans = S = tot = 0;106         T = n + m + 1;107         ans1.clear();108         ans2.clear();109         for(int i = 1; i <= m; ++i){110             scanf("%d",&w);111             add(S,i,w);112             ans += w;113             go(i);114         }115         for(int i = 1; i <= n; ++i){116             scanf("%d",&w);117             add(m+i,T,w);118         }119         ans -= dinic();120         /*memset(vis,false,sizeof(vis));121         dfs(S);122         sort(ans1.begin(),ans1.end(),cmp);123         sort(ans2.begin(),ans2.end(),cmp);124         for(int i = ans1.size()-1; i >= 0; --i)125             printf("%d%c",ans1[i],i?‘ ‘:‘\n‘);126         for(int i = ans2.size()-1; i >= 0; --i)127             printf("%d%c",ans2[i],i?‘ ‘:‘\n‘);*/128         printf("%d\n",ans);129     }130     return 0;131 }
View code

 

Nyist 531 space flight plan

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.