样例说明:

n(城市数目)   m(工程队数目)

每个工程队上交的税收 val[i]

k(k个工程)

xi   yi  ci  costi , 工程队ci承包由xi到yi,政府的补贴为costi

注意:如果聘用了工程队c,则所有与工程队c的项目政府都得补贴,并且所有与c相关的工程队也得参与建设(政府补贴)。这里的有关系是指:

工程队c,d承包的项目 xc yc , xd yd中yc==xd。

感觉题意好难理解。理解之后就好办了,这不就是裸的最大权闭合子图吗?不懂请看Amber的《论文最小割模型在信息学竞赛中的应用》。

政府最大获利 = 实际上总的税收-实际上政府的补贴 = 雇佣的公司的税收-雇佣的公司的补贴 = 总税收(sigma(val)) - (未雇佣的公司税收+雇佣公司的补贴)。

显然,(未雇佣的公司税收+雇佣公司的补贴)就是最小割。

建图如下:

对于每个公司,连上源点,流量为val[i]。连上汇点,流量为sigma( cost ) ,(这里的cost由它承包时政府的总补贴)。

对于相互有关系的公司,连流量为inf的边。

跑一次最大流求得最小割。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ char IN;
bool NEG;
inline void Int(int &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
}
inline void LL(ll &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
} /******** program ********************/ const int MAXN = 10005;
const int MAXM = 500005;
const int INF = 1e9; int a[MAXN],b[MAXN],c[MAXN];
int val[MAXN];
int po[MAXN],tol;
int gap[MAXN],dis[MAXN],arc[MAXN],pre[MAXN],cur[MAXN];
int n,m,vs,vt; struct node{
int y,f,next;
}edge[MAXM]; void Add(int x,int y,int f){
edge[++tol].y = y;
edge[tol].f = f;
edge[tol].next = po[x];
po[x] = tol;
}
void add(int x,int y,int f){
Add(x,y,f);
Add(y,x,0);
} int sap(){
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0] = vt;
rep1(i,vt)
arc[i] = po[i]; int ans = 0;
int aug = INF;
int x = vs; while(dis[vs]<vt){
bool ok = false;
cur[x] = aug;
for(int i=arc[x];i;i=edge[i].next){
int y = edge[i].y;
if(edge[i].f>0&&dis[y]+1==dis[x]){
ok = true;
pre[y] = arc[x] = i;
aug = min(aug,edge[i].f);
x = y;
if(x==vt){
ans += aug;
while(x!=vs){
edge[pre[x]].f -= aug;
edge[pre[x]^1].f += aug;
x = edge[pre[x]^1].y;
}
aug = INF;
}
break;
}
}
if(ok)
continue;
int MIN = vt-1;
for(int i=po[x];i;i=edge[i].next)
if(edge[i].f>0&&dis[edge[i].y]<MIN){
MIN = dis[edge[i].y];
arc[x] = i;
}
if(--gap[dis[x]]==0)
break;
dis[x] = ++ MIN;
++ gap[dis[x]];
if(x!=vs){
x = edge[pre[x]^1].y;
aug = cur[x];
}
}
return ans;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int k,cost;
while(true){
Int(n);Int(m);
if(!n&&!m)break;
Clear(po);
tol = 1;
vs = m+1;
vt = vs+1; int sum = 0;
rep1(i,m){
Int(cost);
add(vs,i,cost);
sum += cost;
} Clear(val);
Int(k);
rep(i,k){
Int(a[i]);Int(b[i]);Int(c[i]);Int(cost);
val[c[i]] += cost;
}
rep(i,k)
rep(j,k)
if(i!=j&&b[i]==a[j])
add(c[i],c[j],INF);
rep1(i,m)
add(i,vt,val[i]);
sum -= sap();
printf("%d\n",sum);
} return 0;
}

  

hdu 3917 Road constructions 最大权闭合子图的更多相关文章

  1. hdu 5772 String problem 最大权闭合子图

    String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...

  2. HDU 3879 Base Station(最大权闭合子图)

    将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...

  3. HDU 3917 Road constructions(最小割---最大权闭合)

    题目地址:HDU 3917 这题简直神题意... 题目本身就非常难看懂不说..即使看懂了.也对这题意的逻辑感到无语...无论了.. 就依照那题意上说的做吧... 题意:给你n个城市,m个公司.若干条可 ...

  4. HDU 5855 Less Time, More profit 最大权闭合子图

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5855 Less Time, More profit Time Limit: 2000/1000 MS ...

  5. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

  6. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  7. HDU5855 Less Time, More profit(最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...

  8. hdu3879 Base Station 最大权闭合子图 边权有正有负

    /** 题目:hdu3879 Base Station 最大权闭合子图 边权有正有负 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 题意:给出n个 ...

  9. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

随机推荐

  1. JavaScript 跨域:window.postMessage 实现跨域通信

    JavaScript 跨域方式实现方式有很多,之前,一篇文章中提到了 JSONP 形式实现跨域.本文将介绍 HTML5 新增的 api 实现跨域:window.postMessage . 1 othe ...

  2. js 基于函数伪造的方式实现继承

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. :app:transformResourcesWithMergeJavaResForDebug FAILED

    资源xml有错误,应该排查每一处xml. 如果提示gradle相关错误,需要修改系统级的build.gradle和程序级的build.gradle,比如后者不能缺少:dependencies {    ...

  4. SAE J1850 VPW PWM, SAE J2411 SWC, ISO 11898 CAN, SAE J1708, Chrysler CCD 接口芯片电路

    SAE J1850 VPW 接口芯片电路 SAE J1850 PWM 接口芯片电路 SAE J2411 SWC 接口芯片电路 ISO 11898 CAN 接口芯片电路 CANH 和CANL 上的电容 ...

  5. 如何修改meclipse中的默认浏览器

    window------->preferrences------------>general-------------->web browser---------->选择你要使 ...

  6. jQuery对象入门级介绍

    你是否曾经见过像  $(".cta").click(function(){})这样的JavaScrip代码?或许你还会思考下 $('#X') 是什么,如果看到这些你都觉得摸不着头脑 ...

  7. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  8. onConfigurationChanged is not called&& 翻转屏幕不执行onConfigurationChanged方法&&onConfigurationChanged不执行

    我总结出一句话: 如果target sdk>=13,必须使用如下方式声明activity:android:configChanges="orientation|screenSize&q ...

  9. jqgrid 获取当前页码

    jqgrid 获取当前页码 $('#gridTable').getGridParam('page'); /** *刷新,jqGrid刷新当前列表页代码 */ function refresh(url) ...

  10. 词法分析器Demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lexe ...