bzoj1877 晨跑

Description

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
 
拆点 费用流
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=400+10,maxm=8e4+10,INF=0x3f3f3f3f;
int n,m,S,T;
 
int aa;char cc;
int read() {
    aa=0;cc=getchar();
    while(cc<'0'||cc>'9') cc=getchar();
    while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
    return aa;
}
 
struct Node{
    int x,y,cap,flow,w;
    Node(){}
    Node(int x,int y,int cap,int w):x(x),y(y),cap(cap),w(w){}
}node[maxm];
 
int fir[maxn],nxt[2*maxm],e=1;
void add(int x,int y,int z,int w) {
    node[++e]=Node(x,y,z,w); nxt[e]=fir[x];fir[x]=e;
    node[++e]=Node(y,x,0,-w); nxt[e]=fir[y];fir[y]=e;
}
 
int from[maxn],zz[maxn],dis[maxn];
bool vis[maxn];
bool spfa() {
    int s=1,t=0,x,y,z;
    memset(dis,0x3f3f3f3f,sizeof(dis));
    memset(zz,0,sizeof(zz));
    zz[++t]=S;vis[S]=1;dis[S]=0;
    while(s<=t) {
        x=zz[s%maxn];
        for(y=fir[x];y;y=nxt[y]) {
            z=node[y].y;
            if(node[y].flow>=node[y].cap||dis[z]<=dis[x]+node[y].w) continue;
            dis[z]=dis[x]+node[y].w;from[z]=y;
            if(!vis[z]) {
                vis[z]=1; t++;
                zz[t%maxn]=z;
            }
        }
        s++;vis[x]=0;
    }
    return dis[T]!=INF;
}
 
int now,rs1=0,rs2=0;
int MCMF() {
    while(spfa()) {
        now=1; rs1++;
        for(int i=T;i!=S;i=node[from[i]].x) {
            node[from[i]].flow+=now;
            node[from[i]^1].flow-=now;
            rs2+=node[from[i]].w*now;
        }
    }
    return rs1;
}
 
int main() {
    n=read();m=read();
    int x,y,z;S=n+1;T=n;
    for(int i=2;i<n;++i) add(i+n,i,1,0);
    add(1+n,1,INF,0);add(n+n,n,INF,0);
    for(int i=1;i<=m;++i) {
        x=read();y=read();z=read();
        add(x,y+n,1,z);
    }
    printf("%d ",MCMF()); printf("%d",rs2);
    return 0;
}

  

弱者就是会被欺负呀
原文地址:https://www.cnblogs.com/Serene-shixinyi/p/7445516.html