POJ-1459-Power Network(最大流,Dinic,弧优化,裸题,向前星建图)

题目链接:http://poj.org/problem?id=1459

Description

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= lmax(u,v) of power delivered by u to v. Let Con=Σuc(u) be the power consumed in the net. The problem is to compute the maximum value of Con. 


An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6. 

Input

There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of cmax(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

Output

For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.

Sample Input

2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
         (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
         (0)5 (1)2 (3)2 (4)1 (5)4

Sample Output

15
6

Hint

The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.

题目大意:直接解释样例把:多组样例输入,n有n个节点,si个起点,si个终点,中间有strani个中转点(道路)

首先输入中转点,然后是起点,然后是终点,最后输出这个网络中能够流通的最大流是多少,一个裸的最大流,建一个s,一个t,从s->各个起点,权值为起点的容量,终点到t,权值为终点的容量,中间建边,一次Dinic直接过,这个题太水了,就是题意不好懂,但是懂了题意就超简单。

从开始敲到A总共不到半个小时,1A(水题水的我都不想分类到网络流里。。)。

ac: 

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

struct node{
	int v,w,cost,nxt;
	node(int _v=0,int _w=0,int _nxt=0):
    v(_v),w(_w),nxt(_nxt){}
}edge[500005<<1];
int head[500005],cur[500005],ecnt;
int dis[500005];
int n,m,s,t;
void intt()
{
	clean(head,-1);
	clean(cur,-1);
	ecnt=0;
}
void add(int u,int v,int w)
{
	edge[ecnt]=node(v,w,head[u]);
	head[u]=ecnt++;
	edge[ecnt]=node(u,0,head[v]);
	head[v]=ecnt++;
}
/*---上面的是板子,不用动---*/

bool bfs()
{
	clean(dis,-1);
	dis[s]=0;
	queue<int> que;
	que.push(s);
	while(que.size())
	{
		int u=que.front();
		que.pop();
		if(u==t)
			return 1;
		for(int i=head[u];i+1;i=edge[i].nxt)
		{
			int temp=edge[i].v;
			if(dis[temp]==-1&&edge[i].w>0)
			{
				dis[temp]=dis[u]+1;
				que.push(temp);
			}
		}
	}
	return 0;
}

int dfs(int u,int low)
{
	if(u==t||low==0)
		return low;
	int res=0;
	for(int &i=cur[u];i+1;i=edge[i].nxt)
	{
		int temp=edge[i].v;
		if(dis[temp]==dis[u]+1&&edge[i].w>0)
		{
			int f=dfs(temp,min(low-res,edge[i].w));
			edge[i].w-=f;
			edge[i^1].w+=f;
			res=res+f;
			if(res==low)
				break;
		}
	}
	return res;
}

void dinic()
{
	int ans=0;
	while(bfs())
	{
		for(int i=0;i<=t;++i)
			cur[i]=head[i];
		ans+=dfs(s,INF);
	}
	cout<<ans<<endl;
}

int get_num(char *str,int i)
{
	int ans=0,j=0;
	while(i)
	{
		ans=0;
		if(str[j]<='9'&&str[j]>='0')
		{
			while(str[j]<='9'&&str[j]>='0')
				ans=ans*10+str[j++]-'0';
			--i;
		}
		else
			++j;
	}
	return ans;
}

int main()
{
	int si,ti,trani;
	while(cin>>n>>si>>ti>>trani)
	{
		s=200,t=201;
		intt();
		char str[100];
		int a,b,l;
		
		for(int i=0;i<trani;++i)
		{
			cin>>str;
			a=get_num(str,1);
			b=get_num(str,2);
			l=get_num(str,3);
			add(a,b,l);
//			cout<<a<<" "<<b<<" "<<l<<endl;
		}
		for(int i=0;i<si;++i)
		{
			cin>>str;
			a=get_num(str,1);
			l=get_num(str,2);
			add(s,a,l);
		}
		for(int i=0;i<ti;++i)
		{
			cin>>str;
			a=get_num(str,1);
			l=get_num(str,2);
			add(a,t,l);
		}
		dinic();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81975206