Hdu5855less time, more profit (network stream)

Source: Internet
Author: User

Multi-school network flow problem, at that time too vegetable did not want to come out how to build the side, dragged a long time to write, hey, efficiency is not generally low AH
Test instructions: To n factories, the construction of each plant needs pay[i] The cost, t[i].
for M stores, each store can bring pro[i], but it needs to be supplied by a specific K factory. The
now gives an L, which requires a scheme that minimizes the time it takes to build a plant while satisfying gains greater than or equal to L and yielding as much as possible. The
idea is: First order the time, and then anyway altogether 200 time, enumerate, each time in this timeframe of the factory to judge, if a store connected to the N factory is not in the current enumeration of time, then this store can not be added to the diagram, and then run the network flow is almost, The details are as follows

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace
Std
const int OO=1E9; const int mm=9000000;//The words should be almost 200*200*200 like the const int mn=500;//here the number of points should be the source point of the meeting point, and the middle of the 200 stores and 200 factories, altogether should be 402 point struct
SA1 {int pay,t;} plant[mn];
    struct SA2 {int pro,n;
int NEED[MN];

} SHOP[MN];
int Node,src,dest,edge;
int VER[MM],FLOW[MM],_NEXT[MM];
int TMP[MM];
int HEAD[MN],WORK[MN],DIS[MN],Q[MN];
    void Prepare (int _node,int _src,int _dest) {node=_node,src=_src,dest=_dest;
    for (int i=0; i<node; ++i) head[i]=-1;
edge=0;
    } void Addedge (int u,int v,int c) {ver[edge]=v,flow[edge]=c,_next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,_next[edge]=head[v],head[v]=edge++;
    } bool Dicnic_bfs () {int i,u,v,l,r=0;
    for (i=0; i<node; ++i) dis[i]=-1;
    dis[q[r++]=src]=0; For (l=0, l<r; ++l) for (I=head[u=q[l]]; i>=0; i=_next[i]) if (flow[i]&&dis[v=ver[i]]<0) {dis[q[r++]=v]=dis[u]+1;
            if (v==dest) return 1;
} return 0;
    } int Dicnic_dfs (int u,int exp) {if (u==dest) return exp; for (int &i=work[u],v,tmp; i>=0; i=_next[i]) if (flow[i]&&dis[v=ver[i]]==dis[u]+1&& (TMP=DICNI
            C_dfs (V,min (exp,flow[i))) >0) {flow[i]-=tmp;
            flow[i^1]+=tmp;
        return TMP;
} return 0;
    } int Dicnic_flow () {int i,ret=0,delta;
        while (DICNIC_BFS ()) {for (i=0; i<node; ++i) work[i]=head[i];
    while (Delta=dicnic_dfs (Src,oo)) Ret+=delta;
} return ret;
}//above are the maximum flow dicnic template int t,n,m,l,ans1,ans2,sum;
BOOL VIS[MN],VIS1[MN];
    int main () {//freopen ("In.txt", "R", stdin);

    Freopen ("OUT.txt", "w", stdout);
     Ios::sync_with_stdio (FALSE);
    cin>>t;
    scanf ("%d", &t);
    int Cases=1; while (t--) {//sCANF ("%d%d%d", &n,&m,&l);

        cin>>n>>m>>l;
        memset (plant,0,sizeof (plant));
        memset (shop,0,sizeof (shop));
        memset (tmp,0,sizeof (TMP));  memset (vis,0,sizeof (VIS));//This can not be placed in the next for the loop inside, if each time the initial value is repeated, the number of assigned initial value too much will be timed out, because the order has been ranked, so every time to add more in the line for (int i=1; i<=n;
           i++) {cin>>plant[i].pay>>plant[i].t;
            scanf ("%d%d", &plant[i].pay,&plant[i].t);
        tmp[i]=plant[i].t;
        } sort (tmp+1,tmp+n+1);
            for (int i=1; i<=m; i++) {//scanf ("%d%d", &AMP;SHOP[I].PRO,&AMP;SHOP[I].N);
            cin>>shop[i].pro>>shop[i].n;
                for (int j=1; j<=shop[i].n; j + +) cin>>shop[i].need[j];
        scanf ("%d", &shop[i].need[j]);
        }//Memset (vis,0,sizeof (VIS));
        BOOL flag=0;
        ans1=0;

        ans2=0; for (int i=1; i<=n; i++) {memset (vis1,0,sIzeof (VIS1));//This can not be placed in the next for the loop inside, if each time the initial value is repeated, the number of assigned initial value too much will time out, because the order is already lined up, so every time the number of add in the line for (int j=1; j<=n; j + +)
            {if (Plant[j].t<=tmp[i])//The factory that satisfies the time mark on the vis[j]=1;
            } sum=0;
            Prepare (n+m+2,0,n+m+1);
                for (int j=1; j<=m; J + +) {bool Exi=1; 
                        for (int k=1; k<=shop[j].n; k++) {if (vis[shop[j].need[k]]==0)//If the store does not meet all the factories associated with it, it is assigned 0
                exi=0; } if (Exi)//If the store satisfies the condition {Addedge (0,J,SHOP[J].PRO);//The source point to the store plus edge, the value of the store's profit, this is
                    Disposable Sum+=shop[j].pro;
                        for (int k=1; k<=shop[j].n; k++) {int x=shop[j].need[k];
                            Addedge (J,x+m,oo);//store and associated factory plus edge, the value is infinite if (vis1[x]==0) { AddEdge (X+m,n+m+1,plant[x].pay);//If the current store is not added, then connect it to the meeting point, the weight for the construction of the plant to spend money, and Mark Vis1[x]=1;
            }}}} int Ans=dicnic_flow ();//run over the network stream
                if (sum-ans>=l)//If the profit minus the cost of the money is greater than L, then it is possible, because I was the time to traverse backward, this is the smallest time {flag=1;//mark ans1=tmp[i];//Minimum time ans2=sum-ans;//net profit//cout<<sum<< "" <<ans<<en
                dl
            Break
         }}//printf ("Case #%d:", cases++);
        cout<< "Case #" <<cases++<< ":";
            if (flag)//output time and net profit if present, otherwise output impossible cout<<ans1<< "" <<ans2<<endl;
        printf ("%d%d\n", ans1,ans2);
            else cout<< "Impossible" <<endl;
    printf ("impossible\n");
} return 0;
 }

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.