bzoj1877: [SDOI2009]晨跑

1877: [SDOI2009]晨跑

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 1922  Solved: 1024
[Submit][Status][Discuss]

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。

拆点然后费用流...

 1 #include<bits/stdc++.h>
 2 #define rep(i,l,r) for(int i=l;i<=r;i++)
 3 #define inf 2147483647
 4 #define N 500
 5 using namespace std;
 6 struct Edge{
 7     int to,next,from,c,w;
 8 }e[50000];
 9 int head[N],tot=1,ans,dis[N],from[N],i,n,p,T,num[N][2],sum,m,a,b,c;
10 bool used[N];
11 inline void ins(int u,int v,int w,int cost) {
12      e[++tot].to=v; e[tot].next=head[u]; head[u]=tot; e[tot].w=w; e[tot].c=cost; e[tot].from=u;
13 }
14 inline int left(int x) {
15      return num[x][0];
16 }
17 inline int right(int x) {
18      return num[x][1];
19 }
20 inline bool spfa() {
21      queue<int> q; for(int i=0;i<=T;i++) dis[i]=inf; dis[1]=0; q.push(1); used[1]=1;  
22      while(!q.empty()) {
23           int x=q.front(); q.pop();
24           for(int k=head[x];k;k=e[k].next) 
25            if(e[k].w>0&&dis[x]+e[k].c<dis[e[k].to]){
26                   dis[e[k].to]=dis[x]+e[k].c; from[e[k].to]=k;
27                   if(!used[e[k].to]) {
28                         used[e[k].to]=1; q.push(e[k].to);
29                   }
30             }
31            used[x]=0;
32      }
33      if(dis[T]==inf) return 0;else return 1;
34 }
35 
36 inline void run() {
37      int x=inf;
38      for(int k=from[T];k;k=from[e[k].from]) x=min(x,e[k].w);
39      for(int k=from[T];k;k=from[e[k].from]) {
40           e[k].w-=x; e[k^1].w+=x; ans+=e[k].c*x;
41      }
42 }
43 
44 inline int read() {
45     int x=0; char ch=getchar();
46     while(ch<'0' || ch>'9') ch=getchar();
47     while(ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
48     return x;
49 }
50 inline void insert(int u,int v,int w,int c) {
51      ins(u,v,w,c); ins(v,u,0,-c);
52 }
53 int main () {
54     scanf("%d%d",&n,&m);
55     rep(i,1,n) num[i][0]=++sum,num[i][1]=++sum;
56     insert(left(1),right(1),inf,0); insert(left(n),right(n),inf,0);
57     rep(i,2,n-1) insert(left(i),right(i),1,0); T=right(n);
58     while(m--) {
59         a=read(); b=read(); c=read();
60         insert(right(a),left(b),1,c);
61     }
62     while(spfa()) run();
63     printf("%d %d",e[2^1].w,ans);
64 }
View Code
原文地址:https://www.cnblogs.com/Bloodline/p/5885011.html