Exercise: codevs 1035 train stop problem report, codevs1035

Source: Internet
Author: User

Exercise: codevs 1035 train stop problem report, codevs1035

In this article, I wrote a problem-solving report again. This topic is codevs 1035 train stop.

The question is to give m trains the arrival time, stay time and the value of vehicle-mounted goods. The station has n lanes, when a train stops at a station, it will get 1% of the profits from the value of the vehicle-mounted goods. This allows you to find a way to maximize the profits of the station and output the profits.

The idea of zookeeper is as follows:

At a glance, we can see that the maximum cost of the flow (MCMF)
Apparently cost: Indicates station income
Then ......
How can I create a map using a station as a point to seek flow? The same station may pass through several trains ......, It seems very troublesome ......;
So what are the steps for creating images, connecting edges, and connecting them?
It looks a bit similar to the split point in number 2 in the square ......;
So this can be ...... Take the train as the point, split one point into two, and then establish the stream relationship.
Reach [I] And stay [I] serve as conditions for determining whether different trains can build edges.
Assume train I, divide it into points 2 * I-1 and 2 * I, connect a flow to 1, cost is-cost [I] edge
If reach [I] + stay [I] <reach [j], a line of traffic between 2 * I and 2 * J-1 is 1 and the cost is 0.
Establish the sink point and the starting point S, T, from S to each 2 * I-1 to a flow of 1, the cost is 0 side. From every 2 * I to T, the traffic is 1 and the cost is 0.

How can we limit n lanes? In fact, it is very simple. You only need to create a super settlement point ST, and then connect a line from T to ST with a traffic of n and a fee of 0.

In this way, this question is done, and the code is not long, just 98 lines. It is recommended that you refresh the code capability questions, because NOIP2015 was overwhelmed by the Code capability questions.

Let's talk about the code.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <queue> 7 using namespace std; 8 const int INF = 10000000; 9 const int maxe = 10000;10 const int maxn = 500;11 int n,m,reach[maxn],stay[maxn],cost[maxn],vis[maxn<<1],d[maxn<<1],h[maxn<<1],pre[maxn<<1],rid[maxn],cid[maxn],now;12 double ans;13 struct edge{14     int to,cost,cap,next;15 }tr[maxe];16 inline void init(){17     now = 0;18     memset(h,-1,sizeof(h));19     memset(tr,0,sizeof(tr));20     memset(reach,0,sizeof(reach));21     memset(stay,0,sizeof(stay));22     memset(cost,0,sizeof(cost));23 }24 inline void add(int u,int v,int cap,int cost){25     tr[now].to = v;tr[now].cap = cap;tr[now].cost = cost;tr[now].next = h[u];26     h[u] = now++;27     tr[now].to = u;tr[now].cap = 0;tr[now].cost = -cost;tr[now].next = h[v];28     h[v] = now++;29 }30 bool SPFA(int s,int t,int &flow,int &cost){31     for(int i = s;i <= t;++i){32         d[i] = INF;33     }34     int minflow = INF;35     memset(vis,0,sizeof(vis));36     memset(pre,-1,sizeof(pre));37     deque<int>q;38     d[s] = 0;39     vis[s] = 1;40     q.push_back(s);41     while(!q.empty()){42         int x = q.front();q.pop_front();43         vis[x] = 0;44         for(int i = h[x];i != -1;i = tr[i].next){45             edge e = tr[i];46             if(d[e.to] > d[x] + e.cost && e.cap){47                 d[e.to] = d[x] + e.cost;48                 pre[e.to] = i;49                 minflow = min(minflow,e.cap);50                 if(!vis[e.to]){51                     vis[e.to] = 1;52                     q.push_back(e.to);53                 }54             }55         }56     }57     if(d[t] == INF)return false;58     flow += minflow;59     cost += d[t] * minflow;60     for(int i = t;i != s;i = tr[pre[i]^1].to){61         tr[pre[i]].cap -= minflow;62         tr[pre[i]^1].cap += minflow;63     }64     return true;65 }66 int MCMF(int s,int t){67     int flow = 0,cost = 0;68     while(SPFA(s,t,flow,cost));69     return cost;70 }71 int main(){72     scanf("%d%d",&n,&m);73     init();74     for(int i = 1;i <= m;++i){75         scanf("%d%d%d",&reach[i],&cost[i],&stay[i]);76         rid[i] = 2*i;cid[i] = 2*i-1;77         add(cid[i],rid[i],1,-cost[i]);78     }79     for(int i = 1;i <= m;++i){80         for(int j = 1;j <= m;++j){81             if(j != i)82                 if(reach[i] + stay[i] < reach[j]){83                     add(rid[i],cid[j],1,0);84                 }85         }86     }87     int S = 0,T = 2*m+1,ST = 2*m+2;88     for(int i = 1;i <= m;++i){89         add(S,cid[i],1,0);90     }91     for(int i = 1;i <= m;++i){92         add(rid[i],T,1,0);93     }94     add(T,ST,n,0);95     ans = double(MCMF(S,ST))/100;96     printf("%0.2lf\n",-ans);97     return 0;98 }

If you think it is useful, repost it. If you have any comments, please leave a message in the comment bar below, or send a message to my e-mail PC-worker@outlook.com. Thank you.

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.