BJOJ1877|SDOI2009晨跑|费用流

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
Day1

分析:做费用流做多了此题一看就是拆点费用流。

#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 0x7fffffff
using namespace std;

int n,m,tot=0,dis[201],inq[201],ans=0,ans2=0;
struct node{
       int from,next,to,v,c;
}e[200001]; 
int head[201];
int q[200001],from[201];

void ins(int x,int y,int w,int c)
{
     e[++tot].from=x; e[tot].to=y;
     e[tot].v=w; e[tot].c=c;
     e[tot].next=head[x]; head[x]=tot;
}

void insert(int x,int y,int w,int c)
{ ins(x,y,w,c); ins(y,x,0,-c); }

bool spfa()
{
     int h=0,t=1;
     memset(dis,inf,sizeof(dis));
     q[1]=1; dis[1]=0; inq[1]=1;
     while (h<=t)
     {
           h++;
           int now=q[h];
           int i=head[now];
           while (i)
           {
                 if (e[i].v && dis[now]+e[i].c<dis[e[i].to])
                 {
                            dis[e[i].to]=dis[now]+e[i].c;
                            from[e[i].to]=i;
                            if (!inq[e[i].to]) { q[t++]=e[i].to; inq[e[i].to]=1; }
                 }
                 i=e[i].next;
           }
           inq[now]=0;
     }
     if (dis[n*2]==inf) return 0;
     return 1;
}

void mcf()
{
     int x=inf,i=from[n*2];
     while (i)
     {
           x=min(x,e[i].v);
           i=from[e[i].from];
     }
     ans+=1;
     i=from[n*2];
     while (i)
     {
           ans2+=x*e[i].c;
           e[i].v-=x;
           e[i^1].v+=x;
           i=from[e[i].from];
     }
}

int main()
{
     cin >> n >> m;
     for (int i=1; i<=m; i++)
     {
         int x,y,v;
         cin >> x >> y >> v;
         insert(x+n,y,1,v);
     }
     for (int i=2; i<n; i++) insert(i,i+n,1,0);
     insert(1,1+n,inf,0); insert(n,n+n,inf,0);
     while (spfa()) mcf();
     cout << ans << " " << ans2 << endl;
     return 0;
}
原文地址:https://www.cnblogs.com/Shymuel/p/4656332.html