bzoj1877: [SDOI2009]晨跑

挺裸的最小费用最大流。。。

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define op() clr(head,0);pt=edges;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define qwq(x) for(edge *o=head[x];o;o=o->next)
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return x;
}
const int nmax=405;
const int maxn=100005;
const int inf=0x7f7f7f7f;
struct edge{
	int to,cap,cost;edge *next,*rev;
};
struct oo{
	int cap,cost;
	oo(int cap,int cost):cap(cap),cost(cost){}
};
edge edges[maxn<<1],*pt,*head[nmax],*p[nmax];
int d[nmax],a[nmax];bool inq[nmax];
void add(int u,int v,int d,int w){
	pt->to=v;pt->cap=d;pt->cost=w;pt->next=head[u];head[u]=pt++;
}
void adde(int u,int v,int d,int w){
	add(u,v,d,w);add(v,u,0,-w);head[u]->rev=head[v];head[v]->rev=head[u];
}
oo mincost(int s,int t){
	int flow=0,cost=0;
	while(1){
		clr(d,0x7f),d[s]=0;clr(inq,0);inq[s]=1;a[s]=inf;
		queue<int>q;q.push(s);
		while(!q.empty()){
			int x=q.front();q.pop();inq[x]=0;
			qwq(x) if(o->cap>0&&d[o->to]>d[x]+o->cost){
				int to=o->to;d[to]=d[x]+o->cost;
				a[to]=min(a[x],o->cap);p[to]=o;
				if(!inq[to]) q.push(to),inq[to]=1;
			}
		}
		if(d[t]==inf) break;
		flow+=a[t];cost+=d[t]*a[t];
		int x=t;
		while(x!=s) p[x]->cap-=a[t],p[x]->rev->cap+=a[t],x=p[x]->rev->to;
	}
	return oo(flow,cost);
}
int main(){
	op();
	int n=read(),m=read(),u,v,d;
	REP(i,2,n-1) adde(i,n+i,1,0);
	rep(i,m) u=read(),v=read(),d=read(),u==1?adde(u,v,1,d):adde(u+n,v,1,d);
	/*rep(i,n) {
		qwq(i) printf("%d ",o->to);printf("\n");
	}*/
	oo o=mincost(1,n);
	printf("%d %d\n",o.cap,o.cost);
	return 0;
}

1877: [SDOI2009]晨跑

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 1820  Solved: 974
[Submit][Status][Discuss]

Description

Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑、仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑。 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街道,Elaxia只能从 一个十字路口跑向另外一个十字路口,街道之间只在十字路口处相交。Elaxia每天从寝室出发 跑到学校,保证寝室编号为1,学校编号为N。 Elaxia的晨跑计划是按周期(包含若干天)进行的,由于他不喜欢走重复的路线,所以 在一个周期内,每天的晨跑路线都不会相交(在十字路口处),寝室和学校不算十字路 口。Elaxia耐力不太好,他希望在一个周期内跑的路程尽量短,但是又希望训练周期包含的天 数尽量长。 除了练空手道,Elaxia其他时间都花在了学习和找MM上面,所有他想请你帮忙为他设计 一套满足他要求的晨跑计划。

Input

第一行:两个数N,M。表示十字路口数和街道数。 接下来M行,每行3个数a,b,c,表示路口a和路口b之间有条长度为c的街道(单向)。

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

HINT

对于30%的数据,N ≤ 20,M ≤ 120。
对于100%的数据,N ≤ 200,M ≤ 20000。

Source

[Submit][Status][Discuss]

  

posted @ 2016-07-12 10:27  BBChq  阅读(124)  评论(0编辑  收藏  举报