生活随笔
收集整理的這篇文章主要介紹了
[codevs 1035] 火车停留
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://codevs.cn/problem/1035/
題解:
看到題后第一反應應該是把容量作為限制車站個數的條件。那么就以火車為單位建圖了。
拆點,X->Xi,Xj,建立源點、匯點,源點向每個點Xi連邊,Xj向匯點連邊,Xi向Xj連邊,表示火車進站,所以費用設為-cost,接下來對火車之間的關系進行計算,如果兩輛火車可以進入同一個車站,就從Xj向Yi連邊。為了限制車站個數,建立超級源和超級匯,分別和源點與匯點相連,容量作為車站數量。最后跑最小費用最大流取相反數輸出。
代碼:
總時間耗費: 33ms?
總內存耗費: 744B
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;const int maxn = 200 + 10;
const int maxm = 100 + 10;
const int INF = 1e9 + 7;struct Edge {int from, to, cap, flow, cost;
};int n, s, t;
vector<int> G[maxn];
vector<Edge> edges;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});int m = edges.size();G[from].push_back(m-2);G[to].push_back(m-1);
}int d[maxn], p[maxn], a[maxn];
bool inq[maxn];bool BellmanFord(int& cost) {memset(inq, 0, sizeof(inq));for(int i = s; i <= t; i++) d[i] = INF;d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;queue<int> Q;Q.push(s);while(!Q.empty()) {int x = Q.front(); Q.pop();inq[x] = 0;for(int i = 0; i < G[x].size(); i++) {Edge& e = edges[G[x][i]];if(e.cap > e.flow && d[e.to] > d[x] + e.cost) {d[e.to] = d[x] + e.cost;a[e.to] = min(a[x], e.cap-e.flow);p[e.to] = G[x][i];if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }}}}if(d[t] == INF) return 0;cost += d[t]*a[t];int x = t;while(x != s) {edges[p[x]].flow += a[t];edges[p[x]^1].flow -= a[t];x = edges[p[x]].from;}return 1;
}int MincostMaxflow() {int cost = 0;while(BellmanFord(cost));return cost;
}int reach[maxm], stay[maxm];int main() {int n, m, s0, t0;cin >> n >> m;s = 0; t = m+m+3; t0 = t-2; s0 = t-1;AddEdge(s, s0, n, 0); AddEdge(t0, t, n, 0);for(int i = 1; i <= m; i++) {AddEdge(s0, i, 1, 0); AddEdge(i+m, t0, 1, 0);int cost;cin >> reach[i] >> cost >> stay[i];AddEdge(i, i+m, 1, -cost);}for(int i = 1; i <= m; i++)for(int j = 1; j <= m; j++) if(reach[i] + stay[i] < reach[j])AddEdge(i+m, j, 1, 0);printf("%.2lf\n", -0.01*MincostMaxflow());return 0;
}
總結
以上是生活随笔為你收集整理的[codevs 1035] 火车停留的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。