codevs2306 晨跑

来源:互联网 发布:江西网络问政 编辑:程序博客网 时间:2024/05/01 22:08
#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<string>#include<vector>#include<map>#include<set>#include<queue>#define R0(i,n) for(int i=0;i<n;++i)#define R1(i,n) for(int i=1;i<=n;++i)#define cl(x,c) memset(x,c,sizeof x)#define maxn 1000010#define INF 707406378using namespace std;typedef long long ll;int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}struct Edge {int from, to, cap, flow, cost;};int s, t;int ans1=0,ans2=0;vector<int> G[maxn];vector<Edge> edges;void add(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 bf() {cl(inq,0);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;R0(i,G[x].size()){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;ans1 += a[t];ans2 += 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 main() {int n=read(),m=read();s=1,t=n+n;R1(i,m) {int u = read(),v = read(),w = read();add(u+n,v,1,w);}for(int i=2;i<n;i++)add(i,i+n,1,0);add(1,s+n,INF,0);add(n,t,INF,0);while(bf());printf("%d %d",ans1,ans2);return 0;}

0 0
原创粉丝点击