zoukankan      html  css  js  c++  java
  • [CTSC1999]家园

           [CTSC1999]家园

                              时空限制1000ms / 128MB

    题目描述

    由于人类对自然资源的消耗,人们意识到大约在 2300 年之后,地球就不能再居住了。于是在月球上建立了新的绿地,以便在需要时移民。令人意想不到的是,2177 年冬由于未知的原因,地球环境发生了连锁崩溃,人类必须在最短的时间内迁往月球。现有 n 个太空站位于地球与月球之间,且有 m 艘公共交通太空船在其间来回穿梭。每个太空站可容纳无限多的人,而每艘太空船 i 只可容纳 H[i]个人。每艘太空船将周期性地停靠一系列的太空站,例如:(1,3,4)表示该太空船将周期性地停靠太空站 134134134…。每一艘太空船从一个太空站驶往任一太空站耗时均为 1。人们只能在太空船停靠太空站(或月球、地球)时上、下船。初始时所有人全在地球上,太空船全在初始站。试设计一个算法,找出让所有人尽快地全部转移到月球上的运输方案。

    对于给定的太空船的信息,找到让所有人尽快地全部转移到月球上的运输方案。

    输入输出格式

    输入格式:

    第 1 行有 3 个正整数 n(太空站个数),m(太空船个数)和 k(需要运送的地球上的人的个数)。其中 n<=13 m<=20, 1<=k<=50。

    接下来的 m 行给出太空船的信息。第 i+1 行说明太空船 pi。第 1 个数表示 pi 可容纳的人数 Hpi;第 2 个数表示 pi 一个周期停靠的太空站个数 r,1<=r<=n+2;随后 r 个数是停靠的太空站的编号(Si1,Si2,…,Sir),地球用 0 表示,月球用-1 表示。时刻 0 时,所有太空船都在初始站,然后开始运行。在时刻 1,2,3…等正点时刻各艘太空船停靠相应的太空站。人只有在 0,1,2…等正点时刻才能上下太空船。

    输出格式:

    程序运行结束时,将全部人员安全转移所需的时间输出。如果问题

    无解,则输出 0。

    输入样例

    2 2 1
    1 3 0 1 2
    1 3 1 2 -1

    输出样例
    5
     
     
    从小到大枚举天数动态加边
    每天从S 到 起点连一条, 终点 到 T 连一条。
    连一条从上一天到自己的边
    连一条上一天的飞船今天飞到自己的边
    如果满足最大流 >= K
    就满足条件了,退出
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <queue>
      6 #define LL long long
      7 
      8 using namespace std;
      9 
     10 const int MAXN = 1e6 + 10;
     11 const int MAXM = 2e5 + 10;
     12 const LL inf = 2e18;
     13 
     14 int N, M, K; 
     15 
     16 int S, T;
     17 queue<int> q;
     18 LL maxflow = 0;
     19 int cnt = 0;
     20 
     21 int head[MAXN], h[MAXN];
     22 
     23 struct plane {
     24     LL cap;
     25     int num;
     26     int r[40]; 
     27 } p[50];
     28 
     29 int day = 0;
     30 
     31 inline LL read()
     32 {
     33     LL x = 0, w = 1; char ch = 0;
     34     while(ch < '0' || ch > '9') {
     35         if(ch == '-') {
     36             w = -1;
     37         }
     38         ch = getchar();
     39     }
     40     while(ch >= '0' && ch <= '9') {
     41         x = x * 10 + ch - '0';
     42         ch = getchar();
     43     }
     44     return x * w;
     45 }
     46 
     47 struct edge {
     48     int v;
     49     LL w;
     50     int next;
     51 } g[MAXM * 2];
     52 
     53 bool BFS()
     54 {
     55     memset(h, -1, sizeof h);
     56     q.push(S);
     57     h[S] = 0;
     58     while(!q.empty()) {
     59         int t = q.front();
     60         q.pop();
     61         for(int j = head[t]; j != -1; j = g[j].next) {
     62             int to = g[j].v;
     63             if(h[to] == -1 && g[j].w) {
     64                 h[to] = h[t] + 1;
     65                 q.push(to);
     66             }
     67         }
     68     }
     69     return h[T] != -1;
     70 }
     71 
     72 void addedge(int u, int v, int w)
     73 {
     74     g[cnt].v = v;
     75     g[cnt].next = head[u];
     76     g[cnt].w = w;
     77     head[u] = cnt++;
     78 }
     79 
     80 LL DFS(int x, LL f)
     81 {
     82     LL used = 0;
     83     if(x == T) {
     84         return f;
     85     }
     86     for(int j = head[x]; j != -1; j = g[j].next) {
     87         int to = g[j].v;
     88         if(h[to] == h[x] + 1 && g[j].w) {
     89             LL w = f - used;
     90             w = DFS(to, min(w, g[j].w));
     91             g[j].w -= w, g[j ^ 1].w += w;
     92             used += w;
     93             if(used == f) {
     94                 break;
     95             }
     96         }
     97     }
     98     if(used == 0) {
     99         h[x] = -1;
    100     }
    101     return used; 
    102 }
    103 
    104 void add()
    105 {
    106     addedge(S, (day - 1) * N + 1, inf);
    107     addedge((day - 1) * N + 1, S, 0);
    108     addedge(day * N + N, T, inf);
    109     addedge(T, day * N + N, 0);
    110     for(int i = 1; i <= N; i++) {
    111         addedge((day - 1) * N + i, day * N + i, inf);
    112         addedge(day * N + i, (day - 1) * N + i, 0);
    113     }
    114     for(int i = 1; i <= M; i++) {
    115         int l = p[i].r[(day - 1) % p[i].num + 1], r = p[i].r[day % p[i].num + 1];
    116         addedge((day - 1) * N + l, day * N + r, p[i].cap);
    117         addedge(day * N + r, (day - 1) * N + l, 0); 
    118     }
    119 }
    120 
    121 int main()
    122 {
    123     memset(head, -1, sizeof head);
    124     N = read(), M = read(), K = read();
    125     for(int i = 1; i <= M; i++) {
    126         p[i].cap = read();
    127         p[i].num = read();
    128         for(int j = 1; j <= p[i].num; j++) {
    129             p[i].r[j] = read() + 1;
    130             if(p[i].r[j] == 0) {
    131                 p[i].r[j] = N + 2;
    132             }
    133         }
    134     }
    135     N = N + 2;
    136     S = 1e6 + 1, T = 1e6 + 2;
    137     day = 0;
    138     while(maxflow < K) {
    139         day++;
    140         add();
    141         while(BFS()) {
    142             maxflow += DFS(S, inf);
    143         }
    144         if(day >= 300) {
    145             cout<<0;
    146             return 0;
    147         }
    148     }
    149     printf("%d
    ", day);
    150 }
    151 
    152 /*
    153 2 2 1
    154 1 3 0 1 2
    155 1 3 1 2 -1
    156 
    157 */
    View Code
  • 相关阅读:
    C#引用类型详细剖析(转)
    wcf问题集锦
    Emgu CV 初试
    C#语言使用习惯
    多线程和消息机制
    ArrayAdapter
    SimpleAdapter
    删除对话框
    HTML制作个人简历
    冒泡排序
  • 原文地址:https://www.cnblogs.com/wuenze/p/8624835.html
Copyright © 2011-2022 走看看