zoukankan      html  css  js  c++  java
  • 洛谷 2762 太空飞行计划问题【网络流】

    题目链接:洛谷 2762 太空飞行计划问题

    题目描述

    W 教授正在为国家航天中心计划一系列的太空飞行。每次太空飞行可进行一系列商业性实验而获取利润。现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的全部仪器的集合I={I1,I2,…In}。实验Ej需要用到的仪器是I的子集RjÍI。配置仪器Ik的费用为ck美元。实验Ej的赞助商已同意为该实验结果支付pj美元。W教授的任务是找出一个有效算法,确定在一次太空飞行中要进行哪些实验并因此而配置哪些仪器才能使太空飞行的净收益最大。这里净收益是指进行实验所获得的全部收入与配置仪器的全部费用的差额。

    对于给定的实验和仪器配置情况,编程找出净收益最大的试验计划。

    输入输出格式

    输入格式:

    第1行有2 个正整数m和n。m是实验数,n是仪器数。接下来的m 行,每行是一个实验的有关数据。第一个数赞助商同意支付该实验的费用;接着是该实验需要用到的若干仪器的编号。最后一行的n个数是配置每个仪器的费用。

    输出格式:

    第1 行是实验编号;第2行是仪器编号;最后一行是净收益。

    输入输出样例

    输入样例#1:
    2 3
    10 1 2
    25 2 3
    5 6 7
    输出样例#1:
    1 2
    1 2 3
    17

    说明

    感谢@zhouyonglong 提供spj

    n,m<=50

    //最大权闭合子图问题。注意输入。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <sstream>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <queue>
     8 #define CLR(a,b) memset((a),(b),sizeof((a)))
     9 using namespace std;
    10 typedef long long ll;
    11 const int N = 105;
    12 const int inf = 0x3f3f3f3f;
    13 int n, m, S, T;
    14 int dep[N], cur[N];
    15 int head[N];
    16 struct Edge{
    17     int v, c, nex;
    18     Edge(int _v=0,int _c=0,int _nex=0):v(_v),c(_c),nex(_nex){}
    19 };
    20 vector<Edge>E;
    21 void add(int u,int v,int c){E.push_back(Edge(v,c,head[u]));head[u]=E.size()-1;}
    22 
    23 bool bfs() {
    24     queue<int> q;
    25     CLR(dep, -1);
    26     q.push(S); dep[S] = 0;
    27     while(!q.empty()) {
    28         int u = q.front(); q.pop();
    29         for(int i = head[u]; ~i; i = E[i].nex) {
    30             int v = E[i].v;
    31             if(E[i].c && dep[v] == -1) {
    32                 dep[v] = dep[u] + 1;
    33                 q.push(v);
    34             }
    35         }
    36     }
    37     return dep[T] != -1;
    38 }
    39 int dfs(int u, int flow) {
    40     if(u == T) return flow;
    41     int w, used=0;
    42     for(int i = head[u]; ~i; i = E[i].nex) {
    43         int v = E[i].v;
    44         if(dep[v] == dep[u] + 1) {
    45             w = flow - used;
    46             w = dfs(v, min(w, E[i].c));
    47             E[i].c -= w;  E[i^1].c += w;
    48             if(v) cur[u] = i;
    49             used += w;
    50             if(used == flow) return flow;
    51         }
    52     }
    53     if(!used) dep[u] = -1;
    54     return used;
    55 }
    56 int dinic() {
    57     int ans = 0;
    58     while(bfs()) {
    59         for(int i = 0; i <= T;i++)
    60             cur[i] = head[i];
    61         ans += dfs(S, inf);
    62     }
    63     return ans;
    64 }
    65 string s;
    66 stringstream ss;
    67 int main() {
    68     CLR(head, -1); E.clear();
    69     int i, j, x, ans = 0;
    70     scanf("%d%d", &n, &m);
    71     S = 0; T = n + m + 1;
    72     for(i = 1; i <= n; ++i) {
    73         scanf("%d ", &x); ans += x;
    74         add(S, i, x); add(i, S, 0);
    75         getline(cin, s);
    76         ss.clear(); ss.str(s);
    77         while(1) {
    78             ss >> j;
    79             if(ss.fail())break;
    80             add(i, j + n, inf); add(j+n, i, 0);
    81         }
    82     }
    83     for(i = 1; i <= m; ++i) {
    84         scanf("%d", &x);
    85         add(i + n, T, x); add(T, i + n, 0);
    86     }
    87     ans -= dinic();
    88     for(i = 1; i <= n; ++i)
    89         if(dep[i] != -1) printf("%d ", i);
    90     puts("");
    91     for(i = 1; i <= m; ++i)
    92         if(dep[i+n] != -1) printf("%d ", i);
    93     puts("");
    94     printf("%d
    ", ans);
    95     return 0;
    96 }
    0ms
  • 相关阅读:
    动态语言,别再说不
    你应当如何学习C++(以及编程)(rev#1)
    JobsPirate.com:工作信息搜索引擎
    找工作
    ORM, Code Generation and a bit about MDA[转]
    使用phpmaill发送邮件的例子
    一级域名和二级域名的区别是什么?作用怎样?
    css中,如何设置前景色的透明度?
    dede从www跟目录迁移,网站空间
    一个域名最多能对应几个IP地址?,一个IP地址可以绑定几个域名?
  • 原文地址:https://www.cnblogs.com/GraceSkyer/p/7487216.html
Copyright © 2011-2022 走看看