热门标签 | 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());
}
}


推荐阅读
  • 本文详细介绍了如何在循环双链表的指定位置插入新元素的方法,包括必要的步骤和代码示例。 ... [详细]
  • spring boot使用jetty无法启动 ... [详细]
  • c语言二元插值,二维线性插值c语言
    c语言二元插值,二维线性插值c语言 ... [详细]
  • 处理Android EditText中数字输入与parseInt方法
    本文探讨了如何在Android应用中从EditText组件安全地获取并解析用户输入的数字,特别是用于设置端口号的情况。通过示例代码和异常处理策略,展示了有效的方法来避免因非法输入导致的应用崩溃。 ... [详细]
  • 长期从事ABAP开发工作的专业人士,在面对行业新趋势时,往往需要重新审视自己的发展方向。本文探讨了几位资深专家对ABAP未来走向的看法,以及开发者应如何调整技能以适应新的技术环境。 ... [详细]
  • 本文详细介绍了 `org.apache.tinkerpop.gremlin.structure.VertexProperty` 类中的 `key()` 方法,并提供了多个实际应用的代码示例。通过这些示例,读者可以更好地理解该方法在图数据库操作中的具体用途。 ... [详细]
  • 洛谷 P4009 汽车加油行驶问题 解析
    探讨了经典算法题目——汽车加油行驶问题,通过网络流和费用流的视角,深入解析了该问题的解决方案。本文将详细阐述如何利用最短路径算法解决这一问题,并提供详细的代码实现。 ... [详细]
  • 本文介绍了如何通过C#语言调用动态链接库(DLL)中的函数来实现IC卡的基本操作,包括初始化设备、设置密码模式、获取设备状态等,并详细展示了将TextBox中的数据写入IC卡的具体实现方法。 ... [详细]
  • linux网络子系统分析(二)—— 协议栈分层框架的建立
    目录一、综述二、INET的初始化2.1INET接口注册2.2抽象实体的建立2.3代码细节分析2.3.1socket参数三、其他协议3.1PF_PACKET3.2P ... [详细]
  • 本文详细介绍了Windows网络编程中常用的几个关键结构体,包括sockaddr_in、in_addr和hostent,解释了它们的定义和用途,并提供了实际应用中的示例。 ... [详细]
  • 线段树详解与实现
    本文详细介绍了线段树的基本概念及其在编程竞赛中的应用,并提供了一个具体的线段树实现代码示例。 ... [详细]
  • 本文介绍了如何利用X_CORBA实现远程对象调用,并通过多个示例程序展示了其功能与应用,包括基础的Hello World示例、文件传输工具以及一个完整的聊天系统。 ... [详细]
  • 本文详细介绍了JQuery Mobile框架中特有的事件和方法,帮助开发者更好地理解和应用这些特性,提升移动Web开发的效率。 ... [详细]
  • 本文深入探讨了Go语言中的接口型函数,通过实例分析其灵活性和强大功能,帮助开发者更好地理解和运用这一特性。 ... [详细]
  • 本文档介绍了如何使用ESP32开发板在STA模式下实现与TCP服务器的通信,包括环境搭建、代码解析及实验步骤。 ... [详细]
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社区 版权所有