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

终于AK了,贴一个非常简洁的代码!可供参考!

Posted by 17DSW at 2018-09-12 15:18:05 on Problem 2135
    其实这代码也是根据网上的整理的,已经十分简洁了,绝不废话连篇!47行1300字左右。变量名简单,基本没有定义局部变量,花括号上移,逗号赋值。简洁的代码更有助于学习和参考哦~

#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
#define o(a,b) for(a=0;a<b;++a)
const int M=1e3+3;
const int inf=0x3f3f3f3f;
struct edg{int x,y,f,c,nx;}e[50000];
int n,m,l,h[M],p[M],dis[M],vis[M];
void add(int x,int y,int f,int c){
    e[l].x=x,e[l].y=y,e[l].f=f,e[l].c=c,e[l].nx=h[x],h[x]=l++;
    e[l].x=y,e[l].y=x,e[l].f=0,e[l].c=-c,e[l].nx=h[y],h[y]=l++;
}
bool spfa(int s,int t){
    int i,u,v;
    queue<int>q;
    o(i,n+2)p[i]=-1,vis[i]=0,dis[i]=inf;
    vis[s]=1,dis[s]=0;q.push(s);
    while(!q.empty()){
        u=q.front(),vis[u]=0;q.pop();
        for(i=h[u];i+1;i=e[i].nx)if(e[i].f){
            v=e[i].y;
            if(dis[v]>dis[u]+e[i].c){
                dis[v]=dis[u]+e[i].c;p[v]=i;
                if(!vis[v]){vis[v]=1;q.push(v);}
            }
        }
    }return dis[t]!=inf;
}
int mic(int s,int t){
    int i,f=inf,ans=0;
    while(spfa(s,t)){
        ans+=dis[t];
        for(i=p[t];i+1;i=p[e[i].x])if(f>e[i].f)f=e[i].f;
        for(i=p[t];i+1;i=p[e[i].x])e[i].f-=f,e[i^1].f+=f;
    }return ans;
}
int main(){
    int s,t,i,x,y,c;
    while(~scanf("%d%d",&n,&m)){
        s=l=0,t=n+1;
        o(i,n+2)h[i]=-1;
        o(i,m){scanf("%d%d%d",&x,&y,&c);add(x,y,1,c);add(y,x,1,c);}
        add(s,1,2,0);add(n,t,2,0);
        printf("%d\n",mic(s,t));
    }return 0;
}

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