UVA 10594 Data Flow

Source: Internet
Author: User

Original question:
In the latest labs of IIUC, it requires to send huge amount of data from the local server to the Terminal server. The lab setup isn't yet ready. It requires to write a router program for the best path of data. The problem is all links of the network have a fixed capacity and cannot flow more than that amount of data. Also It takes certain amount of time to send one unit data through the link. To avoid the collision at a time only one data unit can travel i.e. at any instant more than one unit of data cannot trave L Parallel through the network. This May is time consuming but it certainly gives no collision. Each node have sufficient buffering capability so the data can be temporarily stored there. IIUC Management wants the shortest possible time to send all the data from the local server to the final one.

For example, with the above network if anyone wants to send the unit data from A to D, he'll send
Unit data through AD link and then unit data through AB-BD link which'll take 10+70=80
Unit time.
Input
Each of the input starts with the positive integers N (2≤n≤100), M (1≤m≤5000). In next few lines
The link and corresponding propagation time would be given. The links are bidirectional and there would
is at the most one link between the network nodes. In next line there'll be positive integers D, K
Where D is the amount of data to being transferred from 1-st to n-th node and K are the link capacity.
Input is terminated by EOF.
Output
For each dataset, print the minimum possible time of a line to send all the data. If it isn't possible to
Send all the data, print ' impossible. The time can be as large as 10 15.
Sample Input
4 5
1 4 1
1 3 3
3 4 4
1 2 2
2 4 5
20 1
4 4
1 3 3
3 4 4
1 2 2
2 4 5
20 100
4 4
1 3 3
3 4 4
1 2 2
2 4 5
20 1
Sample Output
Impossible.
140
Impossible.(the sample data is incorrect.) )

English:
Give you a graph, and then give you n points and M edge, each side has a starting point to the end point, the weight on the edge represents the cost of transmitting data, and finally gives you two numbers, D and K represent the total amount of data to be transferred and the capacity of all sides respectively.
Ask if you can fully communicate from 1 to n nodes if the minimum cost is output.

#include <bits/stdc++.h> using namespace std;
const int maxn=201;
typedef long Long LL;
const LL LL_MAX=1+10E15;
    struct edge//Edge {int from,to;

ll cap,flow,cost;//out point, in point, capacity, current flow, cost (i.e. weighted value) Edge (int u,int v,ll c,ll f,ll W): From (U), to (v), Cap (c), Flow (f), Price (W) {}};
    struct MCMF {int n,m; vector<edge> edges;//Save table vector<int> g[maxn];//save adjacency int inq[maxn];//Determine if a point is in the queue (SPFA algorithm is used) Lon
    G Long d[maxn];//start to d[i] the shortest path to save the value int p[maxn];//used to record the path, save on an arc ll a[maxn];//find the augmented path after the improved amount void init (int n)//initialization
        {this->n=n;
        for (int i=0;i<=n;i++) g[i].clear ();
    Edges.clear (); } void Addedge (int from,int to,int cap,long long Cost)//Add Edge {Edges.push_back (Edge (From,to,cap,0,cost));
        /forward Edges.push_back (Edge (To,from,0,0,-cost));//reverse m=edges.size ();
    G[from].push_back (m-2);//Save adjacency Relation G[to].push_back (m-1) According to the number of edges; } bool Bellmanford (int s,int t,ll& flow,ll& cost)//shortest path algorithm {for (int i=0;i<=n;i++) D[i]=ll_max;
        memset (inq,0,sizeof (INQ));
        d[s]=0;
        Inq[s]=1;
        p[s]=0;

        A[s]=ll_max;
        Queue<int> Q;
        Q.push (s); while (!
            Q.empty ()) {int U=q.front ();
            Q.pop ();
            inq[u]=0;
                for (int i=0;i<g[u].size (); i++) {edge& e=edges[g[u][i]]; if (e.cap>e.flow&&d[e.to]>d[u]+e.cost)//Find the slack side {d[e.to]=d[u]+e) that satisfies the capacity greater than the flow.
                    Cost
                    P[e.to]=g[u][i];
                    A[e.to]=min (A[u],e.cap-e.flow);
                        if (!inq[e.to])//Whether in the queue {Q.push (e.to);
                    Inq[e.to]=1; }}}} if (D[t]==ll_max)//If D[T] is not updated, equivalent to not finding the augmented path, there is no maximum flow and no minimum cost ret
        Urn false; flow+=a[t];//Updates the maximum stream cost+=d[t]*a[t];//unit traffic multiplied by the unit path length used to calculate the consumption for (int u=t;u!=s;u=edges[p[u]].from)///by using p[] to save the value of the previous edge to the newly found augmentation
        Traffic above the path is updated {edges[p[u]].flow+=a[t];//forward change new edges[p[u]^1].flow-=a[t];//reverse change (implemented with bitwise operation)
    } return true;
        } ll mincostmaxflow (int s,int t,long long& cost)//calculates the minimum consumption amount from S to T, returning the maximum stream {ll flow = 0;
        cost=0;
    while (Bellmanford (s,t,flow,cost));//constantly searching for the shortest augmented path until the return flow is not found;
}
};
MCMF MCMF;
struct node {int f,t,c;};
int n,m,d,k;
Node tmp[5001];
    int main () {Ios::sync_with_stdio (false); while (Cin>>n>>m) {for (int i=1;i<=m;i++) Cin>>tmp[i].f>>tmp[i].t>&gt
        ; tmp[i].c;
        cin>>d>>k;
        Mcmf.init (n+1); for (int i=1;i<=m;i++) {MCMF.
            Addedge (TMP[I].F,TMP[I].T,K,TMP[I].C); MCMF.
        Addedge (TMP[I].T,TMP[I].F,K,TMP[I].C); } MCMF.
 Addedge (0,1,d,0);       Long long ans; ll FLOW=MCMF.
        Mincostmaxflow (0,n,ans); if (flow<d) cout<< "impossible."
        <<endl;
    else cout<<ans<<endl;
} return 0;
 }

idea:
I was stuck in the template for an afternoon, the sample is not right, know that I ran with someone else's code, only to find that the data is wrong.
Bare minimum cost maximum flow, note bidirectional Edge ~

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.