zoukankan      html  css  js  c++  java
  • USACO 5.4 Telecowmunication(最大流+枚举)

    面对最小割之类的题目,完全木想法。。。

    枚举+最大流。。复杂度很大了。。。居然很快的就过了。。

      1 /*
      2 ID: cuizhe
      3 LANG: C++
      4 TASK: telecow
      5 */
      6 #include <cstring>
      7 #include <cstdio>
      8 #include <queue>
      9 #include <algorithm>
     10 using namespace std;
     11 #define INF 0xffffff
     12 struct node
     13 {
     14     int u,v,w,next;
     15 } edge[10001];
     16 int first[1001],flag[1001],dis[1001];
     17 int qu[2001],qv[2001];
     18 int t,n,m,ans;
     19 int sv,ev;
     20 void CL()
     21 {
     22     t = 0;
     23     memset(first,-1,sizeof(first));
     24 }
     25 void add(int u,int v,int w)
     26 {
     27     edge[t].u = u;
     28     edge[t].v = v;
     29     edge[t].w = w;
     30     edge[t].next = first[u];
     31     first[u] = t ++;
     32 
     33     edge[t].u = v;
     34     edge[t].v = u;
     35     edge[t].w = 0;
     36     edge[t].next = first[v];
     37     first[v] = t ++;
     38 }
     39 void build()
     40 {
     41     int i;
     42     CL();
     43     for(i = 0; i < m; i ++)
     44     {
     45         add(qu[i]+n,qv[i],INF);
     46         add(qv[i]+n,qu[i],INF);
     47     }
     48     for(i = 1; i <= n; i ++)
     49     {
     50         if(!flag[i])
     51         {
     52             if(i == sv)
     53                 add(i,i+n,INF);
     54             else if(i == ev)
     55                 add(i,i+n,INF);
     56             else
     57                 add(i,i+n,1);
     58         }
     59     }
     60 }
     61 int bfs()
     62 {
     63     int u,v,i;
     64     memset(dis,-1,sizeof(dis));
     65     queue<int> que;
     66     que.push(sv);
     67     dis[sv] = 0;
     68     while(!que.empty())
     69     {
     70         u = que.front();
     71         que.pop();
     72         for(i = first[u]; i != -1; i = edge[i].next)
     73         {
     74             v = edge[i].v;
     75             if(edge[i].w > 0&&dis[v] < 0)
     76             {
     77                 dis[v] = dis[u] + 1;
     78                 que.push(v);
     79             }
     80         }
     81     }
     82     if(dis[ev] > 0) return 1;
     83     else return 0;
     84 }
     85 int dfs(int u,int step)
     86 {
     87     int i,a = 0,v;
     88     if (u == ev) return step;
     89     for (i = first[u]; i != -1; i = edge[i].next)
     90     {
     91         v = edge[i].v;
     92         if (edge[i].w > 0&& dis[v] == dis[u]+1&&(a = dfs(v,min(step,edge[i].w))))
     93         {
     94             edge[i].w -= a;
     95             edge[i^1].w += a;
     96             return a;
     97         }
     98     }
     99     return 0;
    100 }
    101 int dinic()
    102 {
    103     int res,ans = 0;
    104     while(bfs())
    105     {
    106         while(res = dfs(sv,INF))
    107             ans+= res ;
    108     }
    109     return ans;
    110 }
    111 void path(int x)
    112 {
    113     int i;
    114     if(x == 0) return ;
    115     for(i = 1; i <= n; i ++)
    116     {
    117         if(flag[i] == 1) continue;
    118         if(i == sv||i == ev) continue;
    119         flag[i] = 1;
    120         build();
    121         if(dinic() == x-1)
    122         {
    123             if(x == ans)
    124                 printf("%d",i);
    125             else
    126                 printf(" %d",i);
    127             path(x-1);
    128             return ;
    129         }
    130         else
    131             flag[i] = 0;
    132     }
    133     return ;
    134 }
    135 int main()
    136 {
    137     int i;
    138     freopen("telecow.in","r",stdin);
    139     freopen("telecow.out","w",stdout);
    140     scanf("%d%d%d%d",&n,&m,&sv,&ev);
    141     for(i = 0; i < m; i ++)
    142     {
    143         scanf("%d%d",&qu[i],&qv[i]);
    144     }
    145     build();
    146     printf("%d
    ",ans = dinic());
    147     path(ans);
    148     printf("
    ");
    149     return 0;
    150 }
  • 相关阅读:
    ASP.NET MVC5+EF6+EasyUI 后台管理系统--任务调度系统解析
    ueditor插入自定义内容和样式
    PHP跨页面传递时session失效
    apache 错误:The system cannot find the file specified.
    Thinkphp5 使用odbc连接到sqlserver
    thinkphp5在URL地址里隐藏模块名
    wamp设置自定义域名访问php网站
    wamp因配置错误而导致apache无法启动的问题
    Jquery 实现input回车时跳转到下一个input元素
    PHP ERROR : Call to undefined function curl_init()
  • 原文地址:https://www.cnblogs.com/naix-x/p/3268076.html
Copyright © 2011-2022 走看看