UVA 1658 Admiral

Source: Internet
Author: User

Original question:
Michiel Adriaenszoon de Ruyter is the most famous admiral in Dutch
Role in the Anglo-dutch Wars of the 17th century. De Ruyter personally commanded a flagship and
Issued commands to Allied warships during naval battles.
In De Ruyter's time, graph theory had just been invented and the admiral used it to his great
Advantage in planning he naval battles. Waypoints at sea is represented by vertices, and possible
Passages from one waypoint to another is represented as directed edges. Given any and waypoints
W 1 and W 2, there is at the most one passage W 1→w 2. Each directed edge was marked with the number
of cannonballs that need to being fired in order to safely move a ship along that edge, sinking the enemy
Ships encountered along the.
One of De Ruyter's most successful tactics is the De ruyter manoeuvre. Here, the warships start
At the same waypoint, and split up and fight their to through the enemy fleet, joining up again at a
Destination waypoint. The manoeuvre prescribes that the both warships take disjunct routes, meaning
That they must isn't visit the same waypoint (other than the start and end-points), or use the same
Passage during the battle.
Being Dutch, Admiral De Ruyter did not like to waste money; In 17th century naval warfare, this
meant firing as few expensive cannonballs as possible.

Figure 1: A particular instance of De Ruyter ' s tactic, visualised as A graph. Ships (' Red ')
and ' Blue ') move from a gkfx starting point (1) to a shared endpoint (6). The red ship's route is
1→3→6 (firing canonballs along the "the"); The blue ship's route is 1→2→5→4→6 (firing
Canonballs along the "the"). In total, Canonballs is fired during the manoeuvre. Except for the
Start-and end-point, no vertices or edges is visited by both ships.
Input
For each test case, the input consists of:
a line containing, integers V (3≤v≤1000) and E (3≤e≤10000), the number of waypoints
and passages, respectively.
then, e lines follow:for each passage, a line containing three integers:
1. A I (1≤a i≤v), the starting-point of a passage, which is represented by a waypoint;
2. b I (1≤b i≤v) and (a i̸= b i), the end-point of a passage, which is represented by a waypoint.
All passages is directed passages;
3. c I (1≤c i≤100), the number of cannonballs that is fired when travelling along this passage.
The starting waypoint is 1 and the destination waypoint are v. There is always at least, disjunct
Routes from waypoint 1 to waypoint v.
Output
For each test case, the output consists of a positive integer:the smallest possible sum of cannon-
Balls fired by both ships when reaching the destination waypoint.
Sample Input
6 11
1 2 23
1 3 12
1 4 99
2 5 17
2 6 73
3 5 3
3 6 21
4 6 8
5 2 33
5 4 5
6 5 20
Sample Output
86

English:
Give you a graph with a maximum of 1000 nodes and 10,000 edges. The right side of the value, now let you from the beginning, to the end of the walk two times, each time can not walk the same node. Now ask you two times the weight and the minimum is how much.

#include <bits/stdc++.h> using namespace std;
const int maxn=2000+5;
    struct Edge {int from,to,cap,flow,cost;

Edge (int u,int v,int c,int f,int W): From (U), to (v), Cap (c), Flow (f), Cost (W) {}};
    struct MCMF {int n,m;
    Vector<edge> edges;
    Vector<int> G[MAXN];
    int INQ[MAXN];
    int D[MAXN];
    int P[MAXN];

    int A[MAXN];
        void init (int n) {this->n=n;
        for (int i=1;i<=n;i++) g[i].clear ();
    Edges.clear ();
        } void Addedge (int from,int to,int cap,int cost) {Edges.push_back (Edge (from,to,cap,0,cost));
        Edges.push_back (Edge (to,from,0,0,-cost));
        M=edges.size ();
        G[from].push_back (m-2);
    G[to].push_back (m-1);
            } bool Bellmanford (int s,int t,int& flow,long long& cost) {for (int i=1;i<=n;i++)
        D[i]=int_max;
        memset (inq,0,sizeof (INQ));
        d[s]=0;
        Inq[s]=1;
        p[s]=0;

        A[s]=int_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) {d[e.to]=d[u]+e.cost;
                    P[e.to]=g[u][i];
                    A[e.to]=min (A[u],e.cap-e.flow);
                        if (!inq[e.to]) {Q.push (e.to);
                    Inq[e.to]=1;
        }}}} if (D[t]==int_max) return false;
        FLOW+=A[T];
        cost+= (Long Long) d[t]* (long Long) a[t];
            for (int u=t;u!=s;u=edges[p[u]].from) {edges[p[u]].flow+=a[t];
        EDGES[P[U]^1].FLOW-=A[T];
    } return true; } int mincostmaxflow (int s,int t,long long& Cost) {int flow = 0;
        cost=0;
        while (Bellmanford (s,t,flow,cost));
    return flow;
}
};
MCMF M;
    int main () {Ios::sync_with_stdio (false);
    int n,m,f,t,c;
        while (cin>>n>>m) {m.init (n*2);
        for (int i=2;i<=n-1;i++) {M.addedge (i,i+n,1,0);
        } m.addedge (1,n+1,2,0);
        M.addedge (n,n*2,2,0);

            for (int j=1;j<=m;j++) {cin>>f>>t>>c;
        M.addedge (F+N,T,1,C);
        } Long long cost;
        M.mincostmaxflow (1,n*2,cost);
    cout<<cost<<endl;
} return 0;
 }

Answer:
Similar to UVA 10806, it is solved using the minimum cost flow problem.
But UVA 10806 is not going to go the same side each time, now going to go without the same point.
Now the point is split into two nodes, one edge is connected, the traffic capacity on the edge is set to 1, and the weight is set to 0.
The source point is pressed into the flow 2, and the last incoming flow of the meeting point is 2.

Finally, the minimum cost flow can be calculated.

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.