hdu 3917 Road constructions 最大权闭合子图
样例说明:
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 最大权闭合子图的更多相关文章
- hdu 5772 String problem 最大权闭合子图
String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...
- HDU 3879 Base Station(最大权闭合子图)
将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...
- HDU 3917 Road constructions(最小割---最大权闭合)
题目地址:HDU 3917 这题简直神题意... 题目本身就非常难看懂不说..即使看懂了.也对这题意的逻辑感到无语...无论了.. 就依照那题意上说的做吧... 题意:给你n个城市,m个公司.若干条可 ...
- HDU 5855 Less Time, More profit 最大权闭合子图
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5855 Less Time, More profit Time Limit: 2000/1000 MS ...
- HDU 3879 Base Station(最大权闭合子图)
经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...
- HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...
- HDU5855 Less Time, More profit(最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...
- hdu3879 Base Station 最大权闭合子图 边权有正有负
/** 题目:hdu3879 Base Station 最大权闭合子图 边权有正有负 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 题意:给出n个 ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
随机推荐
- OC:copy 与 retain 的区别
copy与retain的区别: copy是创建一个新对象,retain是创建一个指针,引用对象计数加1.Copy属性表示两个对象内容相同,新的对象retain为1 ,与旧有对象的引用计数无关,旧有对象 ...
- 查找DOM
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring MVC防止数据重复提交
现实开发中表单重复提交的例子很多,就包括手上这个门户的项目也有这种应用场景,用的次数多,但是总结,这还是第一次. 一.基本原理 使用token,给所有的url加一个拦截器,在拦截器里面用java的UU ...
- 为什么无法发起qq临时会话,必须添加好友?如何设置才能临时会话?
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-06-03) 一. QQ等级低于10级不能进行临时会话 按照腾讯的设置,QQ等级如果低于10级(2个月亮+2颗星星),无法发起 ...
- Myeclipse如何整合tomcat
.在本机上安装MyEclipse和Tomcat 5软件程序 2.运行MyEclipse,设置与Tomcat 5服务器的连接,如下图所示: 选择Window--->Preferences,点击进入 ...
- C++ foreach
考虑下面的需求,对vector<int>中的每个元素加1,如何做? void add(int& lhs) // 注意:要修改主调方法中的数据,这里要使用引用 { lhs= lhs ...
- 你应该知道的JavaScript中NaN的秘密
NaN,不是一个数字,是一种特殊的值来代表不可表示的值,使用typeof或其他任何与之比较的处理方式,‘NaN’则会引起一些混乱, 一些操作会导致NaN值的产生.这里有些例子: Math.sqrt(- ...
- Codeforces Round #342 (Div. 2) A - Guest From the Past 数学
A. Guest From the Past 题目连接: http://www.codeforces.com/contest/625/problem/A Description Kolya Geras ...
- Codeforces Round #307 (Div. 2) A. GukiZ and Contest 水题
A. GukiZ and Contest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...
- WCF关于VS2010的配置
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE 下的 devenv.exe.config 在 <configura ...