传送门
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
题解
裸的最小费用最大流,注意拆点。
CODE:
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=1e9;
struct edge
{
int nxt,to,remain,cost;
}a[100000];
int head[1000];
int u[1000];
int dis[1000];
int f[1000];
bool b[1000];
int n,m,x,y,z,num=1,ans1,ans2,S,T;
queue<int>q;
inline int min(int a,int b){return a<b?a:b;}
inline void add(int x,int y,int z,int w)
{
a[++num].nxt=head[x],a[num].to=y,a[num].remain=z,a[num].cost=w,head[x]=num;
a[++num].nxt=head[y],a[num].to=x,a[num].cost=-w,head[y]=num;
}
inline bool SPFA()
{
memset(dis,0x3f,sizeof(dis));
q.push(S);dis[S]=0;u[S]=INF;
while(!q.empty())
{
int tmp=q.front();q.pop();
b[tmp]=0;
for(int i=head[tmp];i;i=a[i].nxt)
if(dis[a[i].to]>dis[tmp]+a[i].cost&&a[i].remain)
{
dis[a[i].to]=dis[tmp]+a[i].cost;
f[a[i].to]=i;
u[a[i].to]=min(u[tmp],a[i].remain);
if(!b[a[i].to]) b[a[i].to]=1,q.push(a[i].to);
}
}
if(dis[T]>INF) return 0;
int p=T;
ans1+=u[T],ans2+=dis[T];
while(p!=S)
{
a[f[p]].remain-=u[T];
a[f[p]^1].remain+=u[T];
p=a[f[p]^1].to;
}
return 1;
}
inline void mcmf(){while(SPFA());}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
add(i,n+i,1,0);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&x,&y,&z),add(x+n,y,1,z);
S=n+1,T=n;
mcmf();
printf("%d %d",ans1,ans2);
return 0;
}