Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

FF超时,思路一样的代码0ms,求解啊~~~~~~无限TLE中。

Posted by GOD_OF_AV at 2011-07-13 15:25:08 on Problem 1273
#include<cstdio>
#include<cstring>
#include<queue>
#include<climits>
#define MAX 250
using namespace std;
int graph[MAX][MAX],link[MAX],n,m;
int bfs()
{
    queue<int> q;
    int x,min[MAX],i;
    min[1]=INT_MAX;
    memset(link,-1,sizeof(link));
    q.push(1);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        if(x==m)
            return min[m];
        for(i=2;i<=m;i++)
            if(graph[x][i]!=0&&link[i]==-1)
                {
                    link[i]=x;
                    min[i]=min[x]<graph[x][i]?min[x]:graph[x][i];
                    q.push(i);
                }
    }
    return 0;
}
int cal()
{
    int x,pre,now,i,ans;
    ans=0;
    while(x=bfs()&&x!=0)
    {
        now=m;
        while(now!=1)
        {
            pre=link[now];
            graph[now][pre]+=x;
            graph[pre][now]-=x;
            now=pre;
        }
        ans+=x;
    }
    return ans;
}
int main()
{
    int i,u,v,cost;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(graph,0,sizeof(graph));
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&cost);
            graph[u][v]+=cost;
        }
        printf("%d\n",cal());
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator