zoukankan      html  css  js  c++  java
  • bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)

    3876: [Ahoi2014]支线剧情

    Time Limit: 10 Sec  Memory Limit: 256 MB
    Submit: 484  Solved: 296
    [Submit][Status][Discuss]

    Description

    【故事背景】
    宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等。不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情。这些游戏往往
    都有很多的支线剧情,现在JYY想花费最少的时间看完所有的支线剧情。
    【问题描述】
    JYY现在所玩的RPG游戏中,一共有N个剧情点,由1到N编号,第i个剧情点可以根据JYY的不同的选择,而经过不同的支线剧情,前往Ki种不同的新的剧情点。当然如果为0,则说明i号剧情点是游戏的一个结局了。
    JYY观看一个支线剧情需要一定的时间。JYY一开始处在1号剧情点,也就是游戏的开始。显然任何一个剧情点都是从1号剧情点可达的。此外,随 着游戏的进行,剧情是不可逆的。所以游戏保证从任意剧情点出发,都不能再回到这个剧情点。由于JYY过度使用修改器,导致游戏的“存档”和“读档”功能损 坏了,
    所以JYY要想回到之前的剧情点,唯一的方法就是退出当前游戏,并开始新的游戏,也就是回到1号剧情点。JYY可以在任何时刻退出游戏并重新开始。不断开始新的游戏重复观看已经看过的剧情是很痛苦,JYY希望花费最少的时间,看完所有不同的支线剧情。

    Input

    输入一行包含一个正整数N。
    接下来N行,第i行为i号剧情点的信息;
    第一个整数为,接下来个整数对,Bij和Tij,表示从剧情点i可以前往剧
    情点,并且观看这段支线剧情需要花费的时间。

    Output

     输出一行包含一个整数,表示JYY看完所有支线剧情所需要的最少时间。

    Sample Input

    6
    2 2 1 3 2
    2 4 3 5 4
    2 5 5 6 6
    0
    0
    0

    Sample Output

    24

    HINT

     JYY需要重新开始3次游戏,加上一开始的一次游戏,4次游戏的进程是


    1->2->4,1->2->5,1->3->5和1->3->6。


    对于100%的数据满足N<=300,0<=Ki<=50,1<=Tij<=300,Sigma(Ki)<=5000

    Source

    By 佚名上传

    【思路】

      有上下界的费用流。

      用最短路算法慢得飞起=-=,等会学一下zkw费用流。

    【代码】

     1 /**************************************************************
     2     Problem: 3876
     3     User: hahalidaxin2
     4     Language: C++
     5     Result: Accepted
     6     Time:10640 ms
     7     Memory:1856 kb
     8 ****************************************************************/
     9  
    10 #include<cstdio>
    11 #include<cstring>
    12 #include<queue>
    13 #include<vector>
    14 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
    15 using namespace std;
    16  
    17 typedef long long LL ;
    18 const int maxn = 1000+10;
    19 const int INF = 1e9;
    20  
    21 struct Edge{ int u,v,cap,flow,cost;
    22 };
    23  
    24 struct MCMF {
    25     int n,m,s,t;
    26     int inq[maxn],a[maxn],d[maxn],p[maxn];
    27     vector<int> G[maxn];
    28     vector<Edge> es;
    29      
    30     void init(int n) {
    31         this->n=n;
    32         es.clear();
    33         for(int i=0;i<n;i++) G[i].clear();
    34     }
    35     void AddEdge(int u,int v,int cap,int cost) {
    36         es.push_back((Edge){u,v,cap,0,cost});
    37         es.push_back((Edge){v,u,0,0,-cost});
    38         m=es.size();
    39         G[u].push_back(m-2);
    40         G[v].push_back(m-1);
    41     }
    42      
    43     bool SPFA(int s,int t,int& flow,LL& cost) {
    44         for(int i=0;i<n;i++) d[i]=INF;
    45         memset(inq,0,sizeof(inq));
    46         d[s]=0; inq[s]=1; p[s]=0; a[s]=INF; 
    47         queue<int> q; q.push(s);
    48         while(!q.empty()) {
    49             int u=q.front(); q.pop(); inq[u]=0;
    50             for(int i=0;i<G[u].size();i++) {
    51                 Edge& e=es[G[u][i]];
    52                 int v=e.v;
    53                 if(e.cap>e.flow && d[v]>d[u]+e.cost) {
    54                     d[v]=d[u]+e.cost;
    55                     p[v]=G[u][i];
    56                     a[v]=min(a[u],e.cap-e.flow);        //min(a[u],..)
    57                     if(!inq[v]) { inq[v]=1; q.push(v);
    58                     }
    59                 }
    60             }
    61         }
    62         if(d[t]==INF) return false;
    63         flow+=a[t] , cost += (LL) a[t]*d[t];
    64         for(int x=t; x!=s; x=es[p[x]].u) {
    65             es[p[x]].flow+=a[t]; es[p[x]^1].flow-=a[t];
    66         }
    67         return true;
    68     }
    69     int Mincost(int s,int t,LL& cost) {
    70         int flow=0; cost=0;
    71         while(SPFA(s,t,flow,cost)) ;
    72         return flow;
    73     }
    74 } mc;
    75  
    76 int n;
    77  
    78 int main() {
    79     scanf("%d",&n);
    80     mc.init(n+2);
    81     int s=0,t=n+1;
    82     mc.AddEdge(t,s,INF,0);
    83     FOR(u,1,n) {
    84         int m,v,c; scanf("%d",&m);
    85         if(u!=1) mc.AddEdge(u,1,INF,0);
    86         mc.AddEdge(u,t,m,0);
    87         FOR(j,1,m) {
    88             scanf("%d%d",&v,&c);
    89             mc.AddEdge(u,v,INF,c);
    90             mc.AddEdge(s,v,1,c);
    91         }
    92     }
    93     LL cost;
    94     mc.Mincost(s,t,cost);
    95     printf("%lld
    ",cost);
    96     return 0;
    97 }
    最短路算法

    【代码2】

      1 /**************************************************************
      2     Problem: 3876
      3     User: hahalidaxin2
      4     Language: C++
      5     Result: Accepted
      6     Time:1884 ms
      7     Memory:1852 kb
      8 ****************************************************************/
      9  
     10 #include<cstdio>
     11 #include<cstring>
     12 #include<queue>
     13 #include<vector>
     14 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
     15 using namespace std;
     16  
     17 typedef long long LL ;
     18 const int maxn = 1000+10;
     19 const int INF = 1e9;
     20  
     21 struct Edge{ int u,v,cap,flow,cost;
     22 };
     23 struct zkw {
     24     int n,m,s,t;
     25     int vis[maxn],d[maxn];
     26     vector<int> G[maxn];
     27     vector<Edge> es;
     28      
     29     void init(int n) {
     30         this->n=n;
     31         es.clear();
     32         for(int i=0;i<n;i++) G[i].clear();
     33     }
     34     void AddEdge(int u,int v,int cap,int cost) {
     35         es.push_back((Edge){u,v,cap,0,cost});
     36         es.push_back((Edge){v,u,0,0,-cost});
     37         m=es.size();
     38         G[u].push_back(m-2);
     39         G[v].push_back(m-1);
     40     }
     41     bool spfa() {
     42         memset(vis,0,sizeof(vis));
     43         for(int i=0;i<n;i++) d[i]=INF;
     44         queue<int> q;
     45         d[t]=0 , vis[t]=1 , q.push(t);
     46         while(!q.empty()) {
     47             int u=q.front(); q.pop() , vis[u]=0;
     48             for(int i=0;i<G[u].size();i++) {
     49                 Edge& e=es[G[u][i]];
     50                 int v=e.v;
     51                 if(es[G[u][i]^1].cap && d[v]>d[u]-e.cost) {
     52                     d[v]=d[u]-e.cost;
     53                     if(!vis[v]) {
     54                         vis[v]=1;
     55                         q.push(v);
     56                     }
     57                 }
     58             }
     59         }
     60         return d[s]!=INF;
     61     }
     62     int dfs(int u,int a,LL& cost) {
     63         vis[u]=1;  if(u==t) return a;
     64         int used=0,w;
     65         for(int i=0;i<G[u].size();i++) {
     66             Edge& e=es[G[u][i]];
     67             int v=e.v;
     68             if(d[u]-e.cost==d[v] && !vis[v] && e.cap) {
     69                 w=dfs(v,min(a-used,e.cap),cost);
     70                 cost+=w*e.cost;
     71                 e.cap-=w , es[G[u][i]^1].cap+=w;
     72                 used+=w; if(used==a) return a;
     73             }
     74         }
     75         return used;
     76     }
     77     int Mincost(int s,int t,LL& cost) {
     78         this->s=s , this->t=t;
     79         int flow=0; cost=0;
     80         while(spfa()) {
     81             vis[t]=1;
     82             while(vis[t]) {
     83                 memset(vis,0,sizeof(vis));
     84                 flow+=dfs(s,INF,cost);
     85             }
     86         }
     87         return flow;
     88     }
     89 } mc;
     90  
     91 int n;
     92  
     93 int main() {
     94     scanf("%d",&n);
     95     mc.init(n+2);
     96     int s=0,t=n+1;
     97     mc.AddEdge(t,s,INF,0);
     98     FOR(u,1,n) {
     99         int m,v,c; scanf("%d",&m);
    100         if(u!=1) mc.AddEdge(u,1,INF,0);
    101         mc.AddEdge(u,t,m,0);
    102         FOR(j,1,m) {
    103             scanf("%d%d",&v,&c);
    104             mc.AddEdge(u,v,INF,c);
    105             mc.AddEdge(s,v,1,c);
    106         }
    107     }
    108     LL cost;
    109     mc.Mincost(s,t,cost);
    110     printf("%lld
    ",cost);
    111     return 0;
    112 }
    zkw费用流

      跑稠密图的话,zkw的优化效果还是比较明显的。

    UPD.16/4/8

    按理说有上下界的处理和跑网络流的那几道题是一样的,现在没时间测了,该上车了。

  • 相关阅读:
    【Rust】多种错误类型
    【Rust】Result别名
    【Rust】Option然后
    【Rust】可选和错误
    【Rust】Result问号
    【Rust】Option转换
    【Rust】Option展开
    【Rust】Result结果
    【Rust】Result提前返回
    jQuery过滤 安静点
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5079068.html
Copyright © 2011-2022 走看看