Road constructions

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1590    Accepted Submission(s): 534

Problem Description
N cities are required to connect with each other by a new transportation system. After several rounds of bidding, we have selected M constructions companies and 
decided which section is assigned to which company, the associated cost and the direction of each road.

Due to the insufficiency of national fiscal revenue and special taxation system (the tax paid by each company pays is a fixed amount and tax payment occurs at the
beginning of the construction of the project)   The government wishes to complete the project in several years and collects as much tax as possible to support the public
expense

For the restrictions of construction and engineering techniques, if a company is required to start the construction, then itself and its associated companies have to 
complete all the tasks they commit (if company A constructs a road 
from city 1 to city 2, company B constructs a road from city 2 to city 3, company C constructs a road from city 1 to city 3, we call 
companies A and B are associated and other company pairs have no such relationship, pay attention, in this example and a are not associated, in other words,’ 
associated' is a directed relationship).   
Now the question is what the maximum income the government can obtain in the first year is?

 
Input
There are multiple cases (no more than 50).
  Each test case starts with a line, which contains 2 positive integers, n and m (1<=n<=1000, 1<=m<=5000).
  The next line contains m integer which means the tax of each company.
  The Third line has an integer k (1<=k<=3000)which indicates the number of the roads.
  Then k lines fellow, each contains 4 integers, the start of the roads, the end of the road, the company is responsible for this road and the cost of the road.
  The end of the input with two zero
 
Output
For each test case output the maximum income in a separate line, and if you can not get any income, please output 0.
 
Sample Input
4 2
500 10
4
1 2 1 10
2 3 1 20
4 3 1 30
1 4 2 60
4 2
500 100
5
1 2 1 10
2 3 1 20
4 3 1 30
4 3 2 10
1 4 2 60
3 1
10
3
1 2 1 100
2 3 1 100
3 1 1 100
0 0
 
Sample Output
440
470
0

Hint

for second test case, if you choose company 2 responsible ways, then you must choose the path of responsible company 1, but if you choose company 1, then you
do not have to choose company 2.

 
Source
 

题意:国家现有K条有向路可能被建设(可建可不建),每个有向路由某个公司担任修建,且国家需要支付建设此路的花费,从M个公司中选出一些公司去完成建设  这些被选出的公司所担任修建的所有相关路,如果A公司修u->2,而B公司修2->v,那么选了A公司也必须选B公(即A公司与B公司有关联)。并且这些被选出的公司需要向国家交不同的税,问国家能得到的利益最多是多少。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
struct edge{
int to,cap,rev;
}; vector<edge> G[5010];
vector<int> st[1010],ed[1010]; int n,m,k,level[5010],sum,iter[5010],cost[5010],tax[5010]; void add_edge(int u,int v,int cap)
{
G[u].push_back(edge{v,cap,G[v].size()});
G[v].push_back(edge{u,0,G[u].size()-1});
} void bfs(int s)
{
queue<int> q;
q.push(s);
level[s]=1;
while(q.size())
{
int now=q.front();q.pop();
for(int i=0;i<G[now].size();i++)
if(G[now][i].cap>0)
{
edge e=G[now][i];
if(level[e.to]<0)
{
level[e.to]=level[now]+1;
q.push(e.to);
}
}
}
}
int dfs(int s,int t,int minn)
{
if(s==t)
return minn;
for(int &i=iter[s];i<G[s].size();i++)
{
edge &e=G[s][i];
if(level[e.to]>level[s]&&e.cap>0)
{
int k=dfs(e.to,t,min(minn,e.cap));
if(k>0)
{
e.cap-=k;
G[e.to][e.rev].cap+=k;
return k;
}
}
}
return 0;
} int max_flow(int s,int t)
{
int ans=0,temp;
for(;;)
{
memset(level,-1,sizeof(level));
bfs(s);
if(level[t]<0)
return ans;
memset(iter,0,sizeof(iter));
while((temp=dfs(s,t,inf))>0)
ans+=temp;
}
return ans;
} void build()
{
for(int i=1;i<=m;i++)
if(cost[i]>0)
{
add_edge(0,i,cost[i]);
sum+=cost[i];
}
else if(cost[i]<0)
add_edge(i,m+1,-cost[i]); for(int u=1;u<=n;u++)
for(int i=0;i<st[u].size();i++)
for(int j=0;j<ed[u].size();j++)
{
int a=st[u][i],b=ed[u][j];
add_edge(b,a,inf);
}
} void init()
{
for(int i=0;i<=m+1;i++) G[i].clear();
for(int i=1;i<=n;i++)
{
st[i].clear();
ed[i].clear();
} MM(cost,0);
MM(tax,0);
} int main()
{
while(~scanf("%d %d",&n,&m)&&(n||m))
{
init();
sum=0;
for(int i=1;i<=m;i++)
scanf("%d",&tax[i]); scanf("%d",&k);
for(int i=1;i<=k;i++)
{
int u,v,b,c;
scanf("%d %d %d %d",&u,&v,&b,&c); st[u].push_back(b);
ed[v].push_back(b);//st与ed的妙用,,刚开始用的麻烦的爆搜。。
cost[b]+=(-c);//切记转换成负数
} for(int i=1;i<=m;i++)
cost[i]+=tax[i];//构建的新图中节点值 build(); printf("%d\n",sum-max_flow(0,m+1));
}
return 0;
}

  分析:很好的一道题,,就是原题题意神坑,,再加上把100看成了10,,太不细心了;

1,将原图上无法进行最大权闭合图,,想想就知道了,,转化成公司上的闭合圈最大图(选a公司那么必须选b公司),

2.最大权闭合图中的sum-maxflow中的sum指的是需要跑最小割的新图中节点中值>0的数之和

hdu 3917 修路与公司 最大权闭合图 好题的更多相关文章

  1. HDU 3061:Battle(最大权闭合图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3061 题意:中文题意. 思路:和上一题神似啊,比上一题还简单,重新看了遍论文让我对这个理解更加深了. 闭合图:如 ...

  2. hdu 4971/ 2014多校/最大权闭合图

    题意:n个项目(每一个相应获得一定价值).m个技术问题(每一个须要支出一定价值),每一个项目必须要攻克若干个技术问题.技术难题之间有拓扑关系. 关键是建图.一看,第一感觉就是最大权闭合图,马上建好了图 ...

  3. hdu 4971 多校10最大权闭合图

    /* 很明显的最大权闭合图题 */ #include<stdio.h> #include<string.h> #include<queue> using names ...

  4. hdu 3879 hdu 3917 构造最大权闭合图 俩经典题

    hdu3879  base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...

  5. hdu 3879 Base Station 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...

  6. hdu 3061 Battle 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3061 由于小白同学近期习武十分刻苦,很快被晋升为天策军的统帅.而他上任的第一天,就面对了一场极其困难的 ...

  7. hdu 3061 hdu 3996 最大权闭合图 最后一斩

    hdu 3061 Battle :一看就是明显的最大权闭合图了,水提......SB题也不说边数多少....因为开始时候数组开小了,WA....后来一气之下,开到100W,A了.. hdu3996. ...

  8. hdu - 4971 - A simple brute force problem.(最大权闭合图)

    题意:n(n <= 20)个项目,m(m <= 50)个技术问题,做完一个项目能够有收益profit (<= 1000),做完一个项目必须解决对应的技术问题,解决一个技术问题须要付出 ...

  9. HDU 3879 && BZOJ 1497:Base Station && 最大获利 (最大权闭合图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3879 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 ...

随机推荐

  1. 设计模式:解释器模式(Interpreter)

    为人处事是一门大学问,察言观色.听懂弦外之音都是非常重要的,老板跟你说“XX你最近表现平平啊,还得要多努力”,如果你不当回事,平常对待,可能下次就是“XX,恩,你人还是不错,平常工作也很努力,但是我想 ...

  2. 客户端实现WebService服务接口

    首先,要获得搭建好的WebService服务的WSDL,如要实现国内手机号码归属地查询WEB服务,其WSDL为:http://ws.webxml.com.cn/WebServices/MobileCo ...

  3. C++游戏服务器编程笔记 IP详解

    C++游戏服务器编程笔记 IP详解 IP详解 INTERNET的历史 上世纪60年底起源于美国 1992年,Internet上的主机超过了100万台 现在已经是现代文明人的必需品    TCP/IP的 ...

  4. GDOI2018游记

    前言 不知怎的,本蒟蒻居然拿到了GDOI参赛名额 于是乎,我稀里糊涂地跟着诸位大佬屁颠屁颠地来到了阔别已久的中山一中 腐败difficult and interesting的GDOI比赛就这样开始了. ...

  5. PYQT5 pyinstaller 打包工程

    win+R 输入cmd  回车 首先安装 pyinstaller : pip install pyinstaller 安装 pywin32: pip install pywin32 在cmd中输入工程 ...

  6. SDX Instance Resource Assignment Guide 1 of 2

    SDX Instance Resource Assignment Guide 1 of 2 Memory and vCPU Requirements for NetScaler VPX https:/ ...

  7. Centos7下安装ZooKeeper

    1.下载源码 zookeeper 需要jdk的支持,需要先安装jdk 官网下载地址: http://mirrors.hust.edu.cn/apache/zookeeper/ 选择最新的版本进行下载 ...

  8. 正确的安装和使用nvm(mac)<转>

    前言 目前主流的node版本管理工具有两种,nvm和n.两者差异挺大的,具体分析可以参考一下淘宝FED团队的一篇文章: 管理 node 版本,选择 nvm 还是 n? 总的来说,nvm有点类似于 Py ...

  9. 免费数学神器Mathpix发布移动版了,一起来写更快的公式

    目录 1. 按 2. 下载地址 3. 介绍和使用 3.1. 介绍 3.2. 实际使用体验 1. 按 本文介绍的Mathpix可用于将手写的公式通过拍照或截图转成LaTeX 表达式. 写博客.记笔记最麻 ...

  10. Golang中的匿名函数(闭包)

    GO语言的匿名函数就是闭包,以下是<GO语言编程>中对闭包的解释 基本概念闭包是可以包含自由(未绑定到特定对象)变量的代码块,这些变量不在这个代码块内或者任何全局上下文中定义,而是在定义代 ...