1877: [SDOI2009]晨跑
Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 1707 Solved: 902
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
费用流建图太差。。
拆点+zkw费用流。。
好像原本没看题解的时候我的建图就是正确的。。
没有狠下心来写。。
附上本蒟蒻的代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define inf 0x7fffffff
int n,m,ans=0,sum=0,cnt=1,head,tail,h[100001],q[100001],dis[100001],T;
bool mark[100001],vis[100001];
struct kx
{
int to,next,v,c;
}edge[100001];
int read()
{
int w=0,c=1; char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-') c=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
w=w*10+ch-'0',ch=getchar();
return w*c;
}
void add(int u,int v,int w,int cost)
{
cnt++,edge[cnt].next=h[u],h[u]=cnt,edge[cnt].to=v,edge[cnt].v=w,edge[cnt].c=cost;
cnt++,edge[cnt].next=h[v],h[v]=cnt,edge[cnt].to=u,edge[cnt].v=0,edge[cnt].c=-cost;
}
bool spfa()
{
int i,now;
memset(vis,false,sizeof(vis));
for (i=1;i<=T;i++)
dis[i]=inf;
head=0,tail=1;
q[0]=T,vis[T]=true,dis[T]=0;
while (head<tail)
{
now=q[head];
head++,vis[now]=false;
for (i=h[now];i;i=edge[i].next)
if (edge[i^1].v && dis[now]-edge[i].c<dis[edge[i].to])
{
dis[edge[i].to]=dis[now]-edge[i].c;
if (!vis[edge[i].to])
{
vis[edge[i].to]=true;
q[tail]=edge[i].to,tail++;
}
}
}
return dis[1]!=inf;
}
int dfs(int x,int f)
{
int w,used=0,i;
mark[x]=true;
if (x==T)
return f;
for (i=h[x];i;i=edge[i].next)
if (dis[edge[i].to]==dis[x]-edge[i].c && edge[i].v && !mark[edge[i].to])
{
w=f-used,w=dfs(edge[i].to,min(w,edge[i].v));
ans+=w*edge[i].c;
edge[i].v-=w,edge[i^1].v+=w;
used+=w;
if (used==f)
return f;
}
return used;
}
int main()
{
int i,x,y,z;
n=read(),m=read();
T=n+n;
for (i=1;i<=m;i++)
{
x=read(),y=read(),z=read();
add(x+n,y,1,z);
}
for (i=2;i<n;i++)
add(i,i+n,1,0);
add(1,n+1,inf,0);
add(n,T,inf,0);
while (spfa())
{
mark[T]=true;
while (mark[T])
{
memset(mark,false,sizeof(mark));
sum+=dfs(1,inf);
}
}
printf("%d %d",sum,ans);
return 0;
}