HDU 3917 Road Constructions (min cut--Maximum weight closure) Classic

Source: Internet
Author: User

Road ConstructionsTime limit:6000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1475 Accepted Submission (s): 489


Problem Descriptionn Cities is required to connect with each and 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 fi Xed Amount and payment occurs at the
Beginning of the construction of the project) the Government wishes to complete the project in several years and collect S as much tax as possible
Expense

For the restrictions of construction and engineering techniques, if a company was required to start the construction, then itself and its associated companies has to
Complete all the tasks they commits (if company a constructs a road
From the City 1 to the City 2, the company B constructs a road from the City 2 to the City 3, the company C constructs a road from the city 1 to the city 3, we call
Companies A and B is associated and other company pairs has no such relationship, pay attention, in this example and a A Re not associated, with other words, '
Associated ' is a directed relationship).
What's the question is, the maximum income the government can obtain in the first year?
Inputthere is multiple cases (no more than 50).
Each test case is 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.
The third line have 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 are responsible for This road and the cost of the road.
The end of the input with the zero
Outputfor each test case output of the maximum income in a separate line, and if you can not get any income, please output 0.
Sample Input
4 2500 1041 2 1 102 3 1 204 3 1 301 4 2 604 2500 10051 2 1 102 3 1 204 3 1 304 3 2 101 4 2 603 11031 2 1 1002 3 1 1003 1 1 1000 0

Sample Output
4404700Hintfor second test case, if you choose Company 2 responsible ways and then you must choose the path of RESPO Nsible Company 1, and if you choose Company 1, then you do not have to choose Company 2.

Source2011 multi-university Training Contest 8-host by hust  test instructions: The state existing K-line can be constructed (can not be built), each having to route a company for construction, And the country needs to pay the cost of building this road, from m companies to select some companies to complete the construction   the selected companies to build all the relevant road, if a company repair u->2, and B company 2->v, Then select a company must also choose b (that is, a company and B Company). And the elected companies need to pay different taxes to the country, asking the country what the most benefit it can get. Problem solving: Build diagram: Vs=0 is the source point, and VT = m+1 is the meeting point. I, J   for the company, tex[I] for the tax, cost[I] is the cost of building road countries for I company. 1:< vs,  i, tex[i] > 2:<  i  ,  vt, cost [i]  >3:<  i  , &NB Sp;j  ,  inf  >   (if I company is associated with J   Company) ans  =  sum{tex[i]:  1<= i <= m} &nb sp;- maxflow (i.e. minimum cut).
#include <stdio.h> #include <string.h> #include <queue> #include <vector> #include <   algorithm>using namespace std; #define Captype intconst int maxn = 5010;    Total number of points const int MAXM = 400010;    Total number of edges const int INF = 1<<30;struct edg{int to,next; Captype Cap;}  Edg[maxm];int Eid,head[maxn];int GAP[MAXN];  The number of points for each distance (or can be considered a height) int DIS[MAXN];  The shortest distance from each point to the end Enode int CUR[MAXN];    Cur[u] indicates that from the U point can flow through the cur[u] number side int pre[maxn];void init () {eid=0; memset (head,-1,sizeof (Head));}    There are three parameters to the edge, 4 parameters void addedg (int u,int v,captype c,captype rc=0) without a forward edge {edg[eid].to=v; edg[eid].next=head[u];  Edg[eid].cap=c;    head[u]=eid++; Edg[eid].to=u;    EDG[EID].NEXT=HEAD[V];  EDG[EID].CAP=RC; head[v]=eid++;}    Captype maxflow_sap (int snode,int enode, int n) {//n is the total number of points including the source and sink points, this must be noted memset (Gap,0,sizeof (GAP));    memset (dis,0,sizeof (dis));    memcpy (cur,head,sizeof (head));    Pre[snode] =-1;    Gap[0]=n;  Captype ans=0;    Max Stream int U=snode; while (dis[snode]<n) {//judgmentFrom the Snode point there is no flow to the next adjacent point if (u==enode) {//Find a way to increase the flow captype min=inf;            int inser; for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to])//To find the maximum amount of traffic Min if (min>=edg[i].cap) {M) from this stream                In=edg[i].cap;            Inser=i;                } for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) {edg[i].cap-=min;  Edg[i^1].cap+=min;            Flow of the side that can be recycled} ans+=min;            u=edg[inser^1].to;        Continue  } bool flag = FALSE;        It is possible to determine whether an int v can flow toward the neighboring point from the U point;            for (int i=cur[u]; i!=-1; i=edg[i].next) {v=edg[i].to;                if (edg[i].cap>0 && dis[u]==dis[v]+1) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue        }//If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of +1 int mind= n for the adjacent flow point; for (int i=head[u]; i!=-1; I=edg[i].nexT) if (edg[i].cap>0 && mind>dis[edg[i].to]) {mind=dis[edg[i].to];        Cur[u]=i;        } gap[dis[u]]--;  if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, then from the source point to the sink point must pass the current point, but the current point is not able to find        Can flow to the point, then the inevitable flow of dis[u]=mind+1;//if a stream of adjacent points is found, the distance is the distance of the adjacent point +1, if not found, then the n+1 gap[dis[u]]++;  if (U!=snode) u=edg[pre[u]^1].to; Return an Edge} return ans; struct node{int to,comp;};    Vector<node>mp[1005];int Main () {int n,m,k,u,v,company,cost[maxn],c;    NODE now;        while (scanf ("%d%d", &n,&m) >0&&n+m!=0) {init ();        int vs = 0, VT = m+1, ans = 0;            for (int i=1; i<=m; i++) {scanf ("%d", &v);            ADDEDG (VS, I, V);            Cost[i] = 0;        Ans + = v;        } for (int i=1; i<=n; i++) mp[i].clear ();        scanf ("%d", &k); while (k--) {scanf ("%d%d%d%d", &u,&v,&company, &c);            Cost[company] + = C; Now.to = v;            Now.comp = Company;        Mp[u].push_back (now);        } for (int i=1; i<=m; i++) Addedg (i, VT, Cost[i]); for (int i=1, i<=n; i++) for (int l=mp[i].size ()-1; l>=0; l--) {u = mp[i][l]                  . to;                  int comp1 = Mp[i][l].comp;                      for (int r=mp[u].size ()-1; r>=0; r--) {int comp2 = Mp[u][r].comp;                      if (COMP1==COMP2) continue;                  ADDEDG (COMP1, COMP2, INF);        }} ans-= MAXFLOW_SAP (VS, VT, vt+1);    printf ("%d\n", ans); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3917 Road Constructions (min cut--Maximum weight closure) Classic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.