• bzoj 1520 [POI2006]Szk-Schools 费用流


    [POI2006]Szk-Schools

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 743  Solved: 381
    [Submit][Status][Discuss]

    Description

    Input

    Output

    如果有可行解, 输出最小代价,否则输出NIE.

    Sample Input

    5
    1 1 2 3
    1 1 5 1
    3 2 5 5
    4 1 5 10
    3 3 3 1

    Sample Output

    9

    HINT

     

    考虑数据范围应该就是比较裸的费用流吧

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstdlib>
      4 #include<algorithm>
      5 #include<cmath>
      6 #include<cstring>
      7 #include<set>
      8 #include<queue>
      9 #include<map>
     10 #define pa pair<int,int>
     11 #define mod 1000000007
     12 #define inf 1000000000
     13 #define ll long long
     14 using namespace std;
     15 int read()
     16 {
     17     int x=0,f=1;char ch=getchar();
     18     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     19     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     20     return x*f;
     21 }
     22 int n,cnt=1,T,tot,ans;
     23 int last[405],h[405],q[405],d[405];
     24 bool inq[405];
     25 struct data{int to,next,c,v;}e[100005];
     26 void ins(int u,int v,int w,int c)
     27 {
     28     e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w;e[cnt].c=c;
     29 }
     30 void insert(int u,int v,int w,int c)
     31 {
     32     ins(u,v,w,c);
     33     ins(v,u,0,-c);
     34 }
     35 bool spfa()
     36 {
     37     memset(inq,0,sizeof(inq));
     38     int head=0,tail=1;
     39     for(int i=0;i<=T;i++)d[i]=inf;
     40     q[0]=T;d[T]=0;inq[T]=1;
     41     while(head!=tail)
     42     {
     43         int now=q[head];head++;if(head==405)head=0;
     44         for(int i=last[now];i;i=e[i].next)
     45             if(e[i^1].v&&d[now]-e[i].c<d[e[i].to])
     46             {
     47                 d[e[i].to]=d[now]-e[i].c;
     48                 if(!inq[e[i].to])
     49                 {
     50                     inq[e[i].to]=1;
     51                     q[tail]=e[i].to;
     52                     tail++;if(tail==405)tail=0;
     53                 }
     54             }
     55         inq[now]=0;
     56     }
     57     return d[0]!=inf;
     58 }
     59 int dfs(int x,int f)
     60 {
     61     inq[x]=1;
     62     if(x==T)return f;
     63     int used=0,w;
     64     for(int i=last[x];i;i=e[i].next)
     65         if(!inq[e[i].to]&&e[i].v&&d[x]-e[i].c==d[e[i].to])
     66         {
     67             w=f-used;
     68             w=dfs(e[i].to,min(e[i].v,w));
     69             ans+=w*e[i].c;
     70             e[i].v-=w;e[i^1].v+=w;
     71             used+=w;if(used==f)return f;
     72         }
     73     return used;
     74 }
     75 void zkw()
     76 {
     77     while(spfa())
     78     {
     79         inq[T]=1;
     80         while(inq[T])
     81         {
     82             memset(inq,0,sizeof(inq));
     83             tot+=dfs(0,inf);
     84         }
     85     }
     86 }
     87 int main()
     88 {
     89     n=read();T=2*n+1;
     90     for(int i=1;i<=n;i++)insert(0,i,1,0);
     91     for(int i=1;i<=n;i++)insert(i+n,T,1,0);
     92     for(int i=1;i<=n;i++)
     93     {
     94         int m=read(),a=read(),b=read(),k=read();
     95         for(int j=a;j<=b;j++)
     96             insert(i,n+j,1,abs((j-m)*k));
     97     }
     98     zkw();
     99     if(tot!=n)puts("NIE");
    100     else printf("%d
    ",ans);
    101     return 0;
    102 }
  • 相关阅读:
    C语言程序设计习题参考答案
    C语言程序设计 数据类型转换
    C语言程序设计 练习题参考答案 第二章
    计算机等级考试二级C语言考试复习五要点
    计算机等级考试二级快速复习法
    C语言程序设计 辗转相除法
    ReportViewer (RDLC) 中的换行符是什么
    关于axis2中对soapfault的处理的一个小bug
    java多线程中利用优先级来分配CPU时间的实例
    马云演讲
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8847396.html
Copyright © 2020-2023  润新知