热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU3917Roadconstructions最大权闭合图2011Multi-UniversityTrainingContest8-HostbyHUST

RoadconstructionsTimeLimit:60002000MS(JavaOthers)MemoryLimit:3276832768K(JavaOthers)T

 

Road constructions Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 313 Accepted Submission(s): 113


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
Hintfor 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 2011 Multi-University Training Contest 8 - Host by HUST
Recommend lcy   只比HDU3879多了一点:公司i是公司j的附属,那么i到j连边,容量为无限大。  
#include
#include
#include
#define N 8005
#define M 1000005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;

int n,m,s,t,num,adj[N],dis[N],q[N];
vector st[1005],ed[1005];
struct edge
{
int v,w,pre;
}e[M];
void insert(int u,int v,int w)
{
e[num]=(edge){v,w,adj[u]};
adj[u]=num++;
e[num]=(edge){u,0,adj[v]};
adj[v]=num++;
}
int bfs()
{
int i,x,v,head=0,tail=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head{
x=q[head++];
for(i=adj[x];~i;i=e[i].pre)
if(e[i].w&&!dis[v=e[i].v])
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
int dfs(int x,int limit)
{
if(x==t)
return limit;
int i,v,tmp,cost=0;
for(i=adj[x];~i&&costif(e[i].w&&dis[x]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
}
else
dis[v]=-1;
}
return cost;
}
int Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(s,inf);
returnans;
}
int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
int i,j,k,u,v,c,w[5005]={0},sum=0;
num=0;
memset(adj,-1,sizeof(adj));
s=0;
t=1;
for(i=1;i<=n;i++)
{
st[i].clear();
ed[i].clear();
}
for(i=1;i<=m;i++)
{
scanf("%d",w+i);
sum+=w[i];
insert(i+1,t,w[i]);
}
scanf("%d",&k);
for(i=1;i<=k;i++)
{
scanf("%d%d%d%d",&u,&v,&c,&w[0]);
insert(s,i+m+1,w[0]);
insert(i+m+1,c+1,inf);
st[u].push_back(c);
ed[v].push_back(c);
}
for(i=1;i<=n;i++)
{
for(j=0;j{
for(k=0;k{
//printf("%d %d %d\n",i,ed[i][j],st[i][k]);
insert(st[i][k]+1,ed[i][j]+1,inf);
}
}
}
printf("%d\n",sum-Dinic());
}
}

  由于选择了一个公司必须选择它的所有项目,所以可以把项目和公司合并为一个点。这样点和边都会减少。
#include
#include
#include
#define N 5005
#define M 50005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;

int n,m,s,t,num,adj[N],dis[N],q[N];
vector st[1005],ed[1005];
struct edge
{
int v,w,pre;
}e[M];
void insert(int u,int v,int w)
{
e[num]=(edge){v,w,adj[u]};
adj[u]=num++;
e[num]=(edge){u,0,adj[v]};
adj[v]=num++;
}
int bfs()
{
int i,x,v,head=0,tail=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head{
x=q[head++];
for(i=adj[x];~i;i=e[i].pre)
if(e[i].w&&!dis[v=e[i].v])
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
int dfs(int x,int limit)
{
if(x==t)
return limit;
int i,v,tmp,cost=0;
for(i=adj[x];~i&&costif(e[i].w&&dis[x]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
}
else
dis[v]=-1;
}
return cost;
}
int Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(s,inf);
returnans;
}
int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
int i,j,k,u,v,c,w[5005]={0},sum=0;
num=0;
memset(adj,-1,sizeof(adj));
s=0;
t=m+1;
for(i=1;i<=n;i++)
{
st[i].clear();
ed[i].clear();
}
for(i=1;i<=m;i++)
{
scanf("%d",w);
sum+=w[0];
insert(i,t,w[0]);
}
scanf("%d",&k);
for(i=1;i<=k;i++)
{
scanf("%d%d%d%d",&u,&v,&c,w);
w[c]+=w[0];
st[u].push_back(c);
ed[v].push_back(c);
}
for(i=1;i<=m;i++)
insert(s,i,w[i]);
for(i=1;i<=n;i++)
for(j=0;jfor(k=0;kif(st[i][k]!=ed[i][j])
insert(st[i][k],ed[i][j],inf);
printf("%d\n",sum-Dinic());
}
}


推荐阅读
  • ProblemDescriptionXiaoAlivesinavillage.Lastyearfloodrainedthevillage ... [详细]
  • fzu 1715 Ball and Box n个不同的求放到m个不同的盒子中方法的个数
    1715BallandBoxAccept:120Submit:288TimeLimit:1000mSecMemoryLimit:32768KBProblem ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • [线段树|平衡树|树状数组]LightOJ - 1087 - Diablo
    1087-DiabloPDF(English)StatisticsForum ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 一、MyEclipse中的一些常用的快捷键:ctrl+shift+x大写ctrl+shift+y小写alt+内容提示写住方法的时候可以先写main然后按alt+就可以了ctrl+1 ... [详细]
  • 联系人:石虎QQ:1224614774昵称:嗡嘛呢叭咪哄*简单约束*CREATETABLEIFNOTEXISTSt_student(idINTEGERPRIMARYKEYAUTOI ... [详细]
author-avatar
张芬921_162
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有