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 仅包含一个整数,表示可以 ...
随机推荐
- 如何在tomcat安装部署php项目
java开发者都知道,tomcat是用来部署java web项目的.前几天老k偶然得知PHP/Java Bridge,通过它可以实现在jsp和php之间共享session,详见<如何实现jsp和 ...
- 圣杯VS双飞翼
双飞翼: <!DOCTYPE html> <html> <head> <title>推荐封面</title> <meta name=& ...
- [转]vector iterator not incrementable 的问题
转自:http://blog.csdn.net/kuaile123/article/details/11105115 vector::erase误使用问题: 暂时使用经验: 不能在循环中使用,否则会报 ...
- 两个ERP 库存调拨
(A) ERP 负责线上销售,公司为扩大规模,发展线下实体 采用另一套ERP(B) A 和B 都是 单独的ERP ,为了使两个ERP 能高效地工作,需开发一个单独衔接模块实现 ,库存的调拨,新品的 ...
- 基本的TCP编程
int socket(int family,int type,int protocol); family: AF_INET ipv4协议 AF_INET6 ipv6协议 AF_LOCAL unix域协 ...
- C#实现XML文件数据库存储
C#实现文件数据库 http://www.cnblogs.com/gaochundong/archive/2013/04/24/csharp_file_database.html#3100076 应用 ...
- influxDB学习总结
1.安装 请参考http://www.cnblogs.com/zhja/p/5996191.html, 安装完毕运行influxd,http://域名:8083为控制台界面:http://域名:808 ...
- SQL Server DATEDIFF() 函数
Server Date 函数 定义和用法 DATEDIFF() 函数返回两个日期之间的天数. 语法 DATEDIFF(datepart,startdate,enddate) startdate 和 e ...
- 关于View端
View--------------Request 1 URL vs n View 同一个URL可以对应多个View, HTML(通过Request请求获得) 例如SAO项目中的step1--> ...
- 如何选择Javascript模板引擎(javascript template engine)?
译者 jjfat 日期:2012-9-17 来源: GBin1.com 随着前端开发的密集度越来越高,Ajax和JSON的使用越来越频繁,大家肯定免不了在前台开发中大量的使用标签,常见到的例子如下: ...