Hdu 3917 Road and the company's biggest right closed graph good problem

Source: Internet
Author: User

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 DescriptionN Cities is required to connect with 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 are A fixed amount and payment occurs at the
Beginning of the construction of the project) The Government wishes to complete the project in several years and Col Lects as much tax as possible-to-support the public
expense

for the restrictions of construction and engineering techniques, if a company was required to start the construction, T Hen 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 C ity 3, we call
companies A and B is associated and other company pairs has no such relationship, pay attention, in this example and A is not associated, in 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).
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 had 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 Responsib Le 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 do not get any income, please output 0.  

Sample Input4 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 0 

Sample Output4404700 Hintfor second Test case, if you choose Company 2 responsible ways and 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. 

Sourcemulti-university Training Contest 8-host by HUST 

Test instructions: The state existing K-road may be built (can not be built), each has to route a company to build, and the country needs to pay the cost of building this road, from m companies to select some companies to complete the construction of these selected companies to build all the relevant road, if a company to repair u-> 2, and B Company Repair 2->v, then choose a company also must choose b public (that is a company and B Company related). And the elected companies need to pay different taxes to the country, asking the country what the most benefit it can get.

#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) (level,-1,sizeof);        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); The magical use of//st and Ed, and the trouble of the first search.  cost[b]+= (-c);//Remember to convert to negative} for (int i=1;i<=m;i++)               cost[i]+=tax[i];//build a new graph in the node value build ();    printf ("%d\n", Sum-max_flow (0,m+1)); } return 0;}

Analysis: Very good one problem, is the original problem test instructions God pit, plus 100 as 10, too careless;

1, the original image can not be the maximum right to close the diagram, think about it, and converted into the company's largest closed circle chart (select a company so must choose B Company),

2. Sum in Sum-maxflow in the maximum-weight closed graph refers to the number of values >0 in the nodes in the new graph that need to run the minimum cut .

Hdu 3917 Road and the company's biggest right closed graph good problem

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.