晨跑

题目描述

Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑、仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑。 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街道,Elaxia只能从 一个十字路口跑向另外一个十字路口,街道之间只在十字路口处相交。Elaxia每天从寝室出发 跑到学校,保证寝室编号为1,学校编号为N。 Elaxia的晨跑计划是按周期(包含若干天)进行的,由于他不喜欢走重复的路线,所以 在一个周期内,每天的晨跑路线都不会相交(在十字路口处),寝室和学校不算十字路 口。Elaxia耐力不太好,他希望在一个周期内跑的路程尽量短,但是又希望训练周期包含的天 数尽量长。 除了练空手道,Elaxia其他时间都花在了学习和找MM上面,所有他想请你帮忙为他设计 一套满足他要求的晨跑计划。

存在的边存在。这种情况下,这条边只能走一次。

输入输出格式

输入格式:

 

第一行:两个数N,M。表示十字路口数和街道数。 接下来M行,每行3个数a,b,c,表示路口a和路口b之间有条长度为c的街道(单向)。

 

输出格式:

 

两个数,第一个数为最长周期的天数,第二个数为满足最长天数的条件下最短的路程长 度。

 

输入输出样例

输入样例#1:
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
输出样例#1:
2 11

说明

对于30%的数据,N ≤ 20,M ≤ 120。

对于100%的数据,N ≤ 200,M ≤ 20000。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define M 0x7fffffff
int n,m,T,stren,INF;
int head[600],nex[90000],to[90000],cap[90000],cost[90000];
int cnt=1,ans,s=1,t,mincost=0,f;
int pre[600],path[600],dis[600],vis[600];
queue<int>q;
void add(int x,int y,int vol,int stren)
{
    to[++cnt]=y;nex[cnt]=head[x];head[x]=cnt;
    cap[cnt]=vol;cost[cnt]=stren;
    to[++cnt]=x;nex[cnt]=head[y];head[y]=cnt;
    cap[cnt]=0;cost[cnt]=-stren;
}
bool spfa( )
{
    memset(pre,-1,sizeof(pre));
    memset(dis,127,sizeof(dis));    
    memset(vis,0,sizeof(vis));
    while(!q.empty())
        q.pop(); 
    INF=dis[t];    
    q.push(s);
    dis[s]=0;
    pre[s]=0;
    vis[s]=1;    
    while( ! q.empty() )
    {                    
        int u=q.front();  q.pop();      
        vis[u]=0;        
        for(int e=head[u] ; e ; e=nex[e] )
        {        
            int v=to[e];                
            if(cap[e]>0&&dis[u]+cost[e]<dis[v])//cost[e]  not cost[v]
            {                
                pre[v]=u;
                dis[v]=dis[u]+cost[e];
                path[v]=e;                            
                if(!vis[v])    vis[v]=1,q.push(v);
            
            }
            //    if(v==t)    break;
        }
    }   
    if(dis[t]>=INF)    return false;
     return true;
}
void minflow()
{    
    while(spfa())
    {            
        mincost+=dis[t];
        ans+=1;
        int k=t;
        while(k!=s)
         {
             cap[path[k]]-=1;
             cap[path[k]^1] +=1;
             k=pre[k];
         } 
    }    
}
int main()
{
    scanf("%d%d",&n,&m);s=1+n; t=n;
    for(int i=1,a,b,c;i<=m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        add(a+n,b,1,c);
    }    
    for(int i=2;i<n;i++)
        add(i,i+n,1,0);
    minflow();
    printf("%d %d",ans,mincost);
    return 0;
}

出了两个错误

  *搞错了下标

  *其中一个数组少了个0

posted @ 2017-05-05 15:21  浪矢-CL  阅读(186)  评论(0编辑  收藏  举报