1877 最小費用最大流
阿新 • • 發佈:2018-12-17
Elaxia最近迷戀上了空手道,他為自己設定了一套健身計劃,比如俯臥撐、仰臥起坐等 等,不過到目前為止,他
堅持下來的只有晨跑。 現在給出一張學校附近的地圖,這張地圖中包含N個十字路口和M條街道,Elaxia只能從 一
個十字路口跑向另外一個十字路口,街道之間只在十字路口處相交。Elaxia每天從寢室出發 跑到學校,保證寢室
編號為1,學校編號為N。 Elaxia的晨跑計劃是按週期(包含若干天)進行的,由於他不喜歡走重複的路線,所以
在一個週期內,每天的晨跑路線都不會相交(在十字路口處),寢室和學校不算十字路 口。Elaxia耐力不太好,
他希望在一個週期內跑的路程儘量短,但是又希望訓練週期包含的天 數儘量長。 除了練空手道,Elaxia其他時間
都花在了學習和找MM上面,所有他想請你幫忙為他設計 一套滿足他要求的晨跑計劃。
Input
第一行:兩個數N,M。表示十字路口數和街道數。
接下來M行,每行3個數a,b,c,表示路口a和路口b之間有條長度為c的街道(單向)。
N ≤ 200,M ≤ 20000。
Output
兩個數,第一個數為最長週期的天數,第二個數為滿足最長天數的條件下最短的路程長 度。
Sample Input
7 10 1 2 1 1 3 1 2 4 1 3 4 1 4 5 1 4 6 1 2 5 5 3 6 6 5 7 1 6 7 1
Sample Output
2 11
題目要求最長週期天數和這個前提下的最短路;
很明顯就是 最小費用最大流;
不過題目有一個約束條件,
每天晨跑的路線不能交叉;
換句話說,一個點只能經過一次;
那麼我們拆點處理:將一個點分為出&入兩種,這中間連流量為1,權值/費用為0即可;
當然對於 起始點為 1和終點為 n 的情況另外處理即可;
然後跑一邊最小費用最大流即可;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 200005 #define inf 0x3f3f3f3f #define INF 999999999999999 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 20100403 #define sq(x) (x)*(x) #define eps 1e-5 typedef pair<int, int> pii; #define pi acos(-1.0) const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } int head[maxn]; struct node { int to, cost, flow, nxt; }edge[maxn]; int cnt = 1; int tot = 0; queue<int>q; void addedge(int from, int to, int flow, int cost) { edge[++cnt].to = to; edge[cnt].cost = cost; edge[cnt].flow = flow; edge[cnt].nxt = head[from]; head[from] = cnt; } int flow[maxn]; int pre[maxn]; int last[maxn]; int mincost = 0; int maxflow = 0; int dis[maxn]; bool vis[maxn]; int n, m; void build(int from, int dt, int cost) { if (from == 1) { addedge(1, dt, 1, cost); addedge(dt, 1, 0, -cost); return; } if (dt == n) { addedge(from + n, dt, 1, cost); addedge(dt, from + n, 0, -cost); return; } addedge(from + n, dt, 1, cost); addedge(dt, from + n, 0, -cost); return; } bool spfa(int s, int t) { memset(dis, 0x7f, sizeof(dis)); ms(vis); memset(flow, 0x7f, sizeof(flow)); q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].nxt) { int to = edge[i].to; if (edge[i].flow > 0 && dis[to] > dis[now] + edge[i].cost) { dis[to] = dis[now] + edge[i].cost; pre[to] = now; last[to] = i; flow[to] = min(flow[now], edge[i].flow); if (!vis[to]) { vis[to] = 1; q.push(to); } } } } return pre[t] != -1; } void mincost_maxflow(int s,int t) { while (spfa(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } } } int main() { //ios::sync_with_stdio(false); rdint(n); rdint(m); memset(head, -1, sizeof(head)); cnt = 1; for (int i = 2; i < n; i++) { addedge(i, i + n, 1, 0); addedge(i + n, i, 0, 0); } for (int i = 1; i <= m; i++) { int x, y, c, d; rdint(x); rdint(y); rdint(d); build(x, y, d); } mincost_maxflow(1, n); cout << maxflow << ' ' << mincost << endl; return 0; }