Codevs 1035 train stay [fee flow] [split]

Source: Internet
Author: User
Tags split time limit

1035 Train Stay
Time limit: 1 s
Space limit: 128000 KB
Topic rank: Master Master

Title Description Description
"Today you are going to travel, send you wind and rain ...", accompanied by the beautiful song, Guo Jing couple finally embark on the journey. In order to reach the frontier as soon as possible to serve the country, they boarded 2002 trains. Can be in the way Sweet station, was the webmaster entangled in the body, what is the reason for it.
Because the station because of poor business, facing bankruptcy, the station in charge of early Huang intelligent, must she help out ideas, save the station.
The station has n lanes, and because of the limited length of the lanes, a maximum of one train can be docked at a time in each lane. The station will have m train from the station every day, in which the first train arrives at the station at the time of Reach[i], the train is loaded with goods of value cost[i].
If the train pulls in, the station will receive a 1% gain of cost[i], but due to the time of shipment, the train will stay at the station for a period of time stay[i], during which time the train will occupy one of the lanes in the station. Of course, the train can not stop at the station and directly out of the station, but so the station will not get a penny.
To save the station, it is to dispatch the train station, so as to maximize the benefits. Of course, to solve this problem is not difficult for Huang Rong, but the border is tight, time waits for people, you can help Huang Rong, let her escape.
Task: Run your program to get the maximum benefit from the station.

Enter a description input Description
The 1th line is two positive integers: N (n≤20) m (m≤100), line 2nd to line m+1, each row has 3 not more than 1000 positive integers. The 3 numbers in line i+1 are: Reach[i], cost[i], and Stay[i], which are separated by a single space.

Outputs description Output Description
There is only one row, which is the maximum benefit for the station (accurate to 2 digits after the decimal point). Note that if train a departs from Lane I, train B just arrives (ie Reach[a]+stay[a] =reach[b]), it cannot enter the I lane.

Sample input to sample
1 3
2 5 1
3 4 1
5 6 2

Sample output Sample Outputs
0.11

Split each train into two points with an intermediate edge capacity of 1, the cost is cost[i], and then the end time of all meet I is earlier than J's start time of the edge I '->j capacity is 1, the cost is 0, and then the maximum cost flow can be.

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <string> # include<iomanip> #include <ctime> #include <climits> #include <cctype> #include <algorithm
> Using namespace std;
const int maxn=100;
    struct Edge {int to,next;
int cap,cost;
}edge[maxn*maxn<<2];
int maxedge=-1;
int head[maxn<<2];
    inline void Addedge (int u,int v,int cap,int cost) {edge[++maxedge]= (edge) {v,head[u],cap,cost};
    Head[u]=maxedge;
    Edge[++maxedge]= (Edge) {u,head[v],0,-cost};
Head[v]=maxedge; } struct Train {int t1,t2;}
TRAIN[MAXN+5];
int N,m,maxnode;
int s,t,s0,t0;
    inline void init () {memset (head,-1,sizeof (head));
    scanf ("%d%d", &n,&m);
    int Reach,cost,stay;
    S= (m<<1) +1,s0=s+1,t0=s0+1,t=t0+1;maxnode=2*m+4;
    Addedge (s,s0,n,0); Addedge (t0,t,n,0); for (int i=1;i<=m;i++) {scanf ("%d%d%d", &reach,&cost,&stay);
        Train[i]= (train) {Reach,stay+reach};
        Addedge (s0,i,1,0); Addedge (i+m,t0,1,0);
    Addedge (I,i+m,1,-cost);
            } for (int i=1;i<=m;i++) for (int j=1;j<=m;j++) {if (i==j) continue;
        if (TRAIN[I].T2&LT;TRAIN[J].T1) Addedge (i+m,j,1,0);
}} bool inque[maxn<<2];
int dis[maxn<<2],add[maxn<<2],pre[maxn<<2],edge_pre[maxn<<2];
    inline bool Spfa () {deque <int> que;
    memset (inque,false,sizeof (Inque));
    memset (dis,0x3f,sizeof (dis));
    Add[s]=0x3f3f3f3f;inque[s]=true;dis[s]=0;que.push_back (S);
        while (!que.empty ()) {int U=que.front (); Que.pop_front (); inque[u]=false;
            for (int i=head[u];~i;i=edge[i].next) {int v=edge[i].to; if (edge[i].cap&&dis[v]>dis[u]+edge[i].cost)//must judge if the cap {dis[v]=dis[u]+ Edge[i].cost;
                Add[v]=min (ADD[U],EDGE[I].CAP);
                Pre[v]=u;edge_pre[v]=i;
                if (Inque[v]) continue;
                Inque[v]=true; if (Que.empty () | | |
                    Dis[v]<dis[que.front ()]) if (!que.empty ()) Que.push_front (v);
                else Que.push_back (v);
            else Que.push_back (v);
    }}} if (dis[t]>=0x3f3f3f3f) return false;//the = needed to be taken!
return true;
    } inline int mcmf () {int flow=0,cost=0;
        while (SPFA ()) {flow+=add[t];
        COST+=ADD[T]*DIS[T];
            for (int i=t;i!=s;i=pre[i]) {int e=edge_pre[i];
        EDGE[E].CAP-=ADD[T];EDGE[E^1].CAP+=ADD[T];
}} return cost;
    } int main () {freopen ("train.in", "R", stdin);
    Freopen ("Train.out", "w", stdout);
    Init ();
    printf ("%.2lf\n", (double)-MCMF ()/100);   
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.