HDU 4494 Teamwork (Multi-source multi-sinks minimum cost maximum flow, ingenious composition)

Source: Internet
Author: User
Tags integer numbers

Teamwork Time limit:2000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): Accepted submission (s): 229


Problem Description Some locations in City A had been destroyed in the fierce battle. So the government decides to send some workers to repair these locations. There is m kinds of workers that were trained for different skills. Each location need some number of some kinds of workers and have a schedule that at what time can the repair begins, and th E Time cost of repair. Any job cannot begin until all the workers required arrived.
For example, location 1 needs 2 workers of type 1 and 3 workers of type 2, and the beginning time and time cost are Ute and minute correspondingly, then 5 workers that satisfy the requirement should arrive before (minute), start work ing at minute and get the job did at minute. Notice that the different types of workers cannot replace each other, so with 3 workers of type 1 and only 2 workers of Ty PE 2, this job cannot is done.
Workers can go from the one location to another after their jobs is done. You can take the Euclidean distance between locations as the time workers need to travel between them. Each worker should is sent from a depot initially at 0 minute. Now your task was to determine the minimum number of workers needed to being sent from depot so, all the jobs can being done.
Input There is multiple test cases, the integer on the first line T (t<25) indicates the number of test cases.
Each test case is begins with a integers n (<=150), the number of location (including the depot) and M (<=5), the Numbe R of different skills.
The next line gives integers x 0, y 0 indicates the coordinate of depot.
Then follows N-1 lines begins with 4 integer numbers:x i, y i, b i (b i>0), p I (P i>0), (x i, y i) gives the coord Inate of the i-th, BI gives the beginning time and PI gives the time cost. The rest of the line gives m non-negative integers v 1, v 2, ..., v M, of which the i-th number indicates the the number O F Workers of type I needed (for all V I, 0<=v i<10, each in least requires one worker).
All integers is less than 1000000 (10 6).
Output for each test cases output one line, the minimum workers to be sent. It's guaranteed that there's always a feasible solution, the all the jobs can being done.
Sample Input

2 4 1 0 0 0 1 1 1 3 1 1 3 3 4 1 0 10 1 5 4 1 0 0 0 1 1 1 3 1 1 3 3 4 1 0 3 1 5
Sample Output
5 9 Test Instructions: There are n local and m skills, each worker can only have one skill, and the number of people who need a variety of different skills in each place varies.

At first the workers were in the No. 0 warehouse, sending workers to the 1~n-1 to work without time, but the workers could run to another place to work after working in one location, waiting for all the people in that location to get to work, and the time needed to run from one location to another was Euclid's distance. , the start time and working time for each location are now given, and workers cannot be replaced (No 2 skilled workers will be able to replace 2 skilled workers), and a minimum of several workers should be dispatched to the warehouse.

Idea: After analyzing the problem, we can find that there is no relationship between workers, because the start time is given, so we must ensure that all the people need to arrive before the start time, so there is no relationship between workers.

Then we enumerate the different skills of the workers, to find out a certain skill of the workers to complete their work at least a few people, and finally summed up is the answer.

But I do not think here will not be the composition = =, really konjac konjac.

The specific composition method is:

First set up the super source point S and the Super meeting point T

S connect all locations, the capacity is INF or the number of people required for this location, the cost is 1 (traffic size is the number of people)

And then all the places are connected to T. This means that a worker is not considered to be moving to another location after it has been done, and it must be the total number of workers in all places.

Then we will now split the location into two points, these points if the time to meet the topic, and even to the corresponding location in the 1~n-1, to find out the number of people to a certain location can be

Concrete implementation can look at the code, should not be ugly to understand

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Cmath> #include <queue> using namespace std;
#define N 1050 #define INF 999999999 #define EPS 1e-8 struct Edge {int u,v,next,cap,cost;} Edge[n*n];
    struct Node {double x,y,b,p;
int num[6];
}e[n];
int cnt,head[n];
Char ma[55][55];
int vis[n],pp[n],d[n],sumflow;
    void Init () {cnt=0;
memset (head,-1,sizeof (head));
    } void Addedge (int u,int v,int cap,int cost) {edge[cnt].u=u;
    Edge[cnt].v=v;
    Edge[cnt].cap=cap;
    Edge[cnt].cost=cost;
    Edge[cnt].next=head[u];

    head[u]=cnt++;
    Edge[cnt].u=v;
    Edge[cnt].v=u;
    Edge[cnt].cap=0;
    Edge[cnt].cost=-cost;
    EDGE[CNT].NEXT=HEAD[V];
head[v]=cnt++;
    } int SPFA (int s,int t,int n) {queue<int>q;
    memset (vis,0,sizeof (VIS));
    memset (pp,-1,sizeof (PP));///pp[i] represents the number of edges on the shortest path ending with I for (int i=0; i<=n; i++) D[i]=inf;
    d[s]=0;
    Vis[s]=1; Q.push (s);
        while (!q.empty ()) {int U=q.front ();
        Q.pop ();
        vis[u]=0;
            for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;
                if (edge[i].cap>0&&d[v]>d[u]+edge[i].cost) {d[v]=d[u]+edge[i].cost;
                Pp[v]=i;
                    if (!vis[v]) {vis[v]=1;
                Q.push (v);
}}}} if (D[t]==inf) return 0;///cannot find a route to the end of return 1;
    } int MCMF (int s,int t,int n) {int mincost=0,minflow,flow=0;///maximum cost, minimum flow in path, total traffic while (SPFA (s,t,n))///find current Longest road
        {minflow=inf+1; for (int i=pp[t]; i!=-1; i=pp[edge[i].u]) minflow=min (MINFLOW,EDGE[I].CAP);///Find the smallest traffic flow+=minflow from the path;
            Total traffic plus minimum traffic for (int i=pp[t]; i!=-1; i=pp[edge[i].u]) {edge[i].cap-=minflow;///current edge minus minimum traffic edge[i^1].cap+=minflow;///Reverse Edge Plus minimum flow} mincost+=d[t]*minflow;///minimum cost equals path and * traffic per path (after how many times)} Sumflow=flow;
return mincost; } Double dist (double x,double y,double x1,double y1) {return sqrt ((x-x1) * (x-x1) + (y-y1) * (y-y1));} int main () {in
    T t,n,m;
    Double x, y;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&m);
        scanf ("%lf%lf", &x,&y);
            for (int i=1;i<n;i++) {scanf ("%lf%lf%lf%lf", &AMP;E[I].X,&AMP;E[I].Y,&AMP;E[I].B,&AMP;E[I].P);
        for (int j=1;j<=m;j++) scanf ("%d", &e[i].num[j]);
        } int ans=0;
            for (int i=1;i<=m;i++) {init ();
            int s=0,t=2*n;
                        for (int j=1;j<n;j++) {Addedge (s,j,e[j].num[i],1);
                        Addedge (j,t,e[j].num[i],0);
                    Addedge (s,j+n,e[j].num[i],0); } for (int j=1;j<n;j++) for (int k=1;k<n;k++) if (J!=k&&dist (E[J].X,E[J].Y,E[K].X,E[K].Y) +e[j].b+e[j].p-e[k].b<eps)
            Addedge (n+j,k,inf,0);
        ANS+=MCMF (s,t,t);
    } printf ("%d\n", ans);
} 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.