描述 Description
Elaxia 最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑、仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑。 现在给出一张学校附近的地图,这张地图中包含 N 个十字路口和 M 条街道,Elaxia 只能从 一个十字路口跑向另外一个十字路口,街道之间只在十字路口处相交。Elaxia 每天从寝室出发 跑到学校,保证寝室编号为 1,学校编号为 N。 Elaxia 的晨跑计划是按周期(包含若干天)进行的,由于他不喜欢走重复的路线,所以 在一个周期内,每天的晨跑路线都不会相交(在十字路口处),寝室和学校不算十字路 口。Elaxia 耐力不太好,他希望在一个周期内跑的路程尽量短,但是又希望训练周期包含的天 数尽量长。 除了练空手道,Elaxia 其他时间都花在了学习和找 MM 上面,所有他想请你帮忙为他设计 一套满足他要求的晨跑计划。
输入格式 InputFormat
第一行:两个数 N,M。表示十字路口数和街道数。 接下来 M 行,每行 3 个数 a,b,c,表示路口 a 和路口 b 之间有条长度为 c 的街道(单向)。
输出格式 OutputFormat
两个数,第一个数为最长周期的天数,第二个数为满足最长天数的条件下最短的路程长 度。
样例输入 SampleInput
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
样例输出 SampleOutput
2 11
除寝室和学校之外路口拆成两个点之间连边容量一费用零。之后有路相连的路口连边容量一费用零,mcmf.
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int inf=0x7fffffff/27.11;
const int maxm=50005;
const int maxn=505;
int i,j,t,n,m,l,r,k,z,y,x;
struct edge
{
int to,fl,co,nx;
}e[maxm];
int head[maxn],dis[maxn],flow[maxn],used[maxn],pre[maxn];
int cnt,num;
deque <int> q;
inline void ins(int u,int v,int f,int c)
{
e[cnt]=(edge){v,f,c,head[u]};head[u]=cnt++;
e[cnt]=(edge){u,0,-c,head[v]};head[v]=cnt++;
}
inline bool spfa(int s,int t,int tim)
{
int i,u,v,w;
while (!q.empty()) q.pop_front();
for (i=1;i<=2*n;i++) dis[i]=inf;
pre[s]=-1;flow[s]=inf;used[s]=tim;dis[s]=0;
q.push_back(s);
while (!q.empty())
{
u=q.front();q.pop_front();used[u]=0;
for (i=head[u];i!=-1;i=e[i].nx)
{
v=e[i].to;w=e[i].co;
if (dis[v]>dis[u]+w && e[i].fl>0)
{
dis[v]=dis[u]+w;
flow[v]=min(flow[u],e[i].fl);
pre[v]=i;
if (used[v]!=tim)
{
if (!q.empty() && dis[v]<dis[q.front()]) q.push_front(v);
else q.push_back(v);
}
}
}
}
return (dis[t]!=inf);
}
inline void mcmf(int s,int t)
{
int i,ans=0,f=0;
while (spfa(s,t,++num))
{
ans+=flow[t]*dis[t];
f++;
for (i=pre[t];i!=-1;i=pre[e[i^1].to]) e[i].fl-=flow[t],e[i^1].fl+=flow[t];
}
printf("%d %d\n",f,ans);
}
int main()
{
cnt=num=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for (i=2;i<=n-1;i++) ins(i,i+n,1,0);
for (i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
ins((x!=1)?(x+n):x,y,1,z);
}
mcmf(1,n);
return 0;
}