poj 1459 Power Network 网络流 ek算法

来源:互联网 发布:大数据与中国发展 编辑:程序博客网 时间:2024/06/17 07:50

这道题做的我好心酸啊,一直T,原来把算法搞错了,需要一个数组记录路径中的流量,而我就是用一个增量保存,所以其他路径上的最小值也会改变当前路径的最小值,因此一直t

吸取教训了


#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;const int inf = 0x3f3f3f3f;struct point {    int c;}p[205][205];int sp,sc;int vis[205];int maxflow;int n;int flow[205];int pre[205];int augment;queue<int> q;void bfs(){    memset(vis,0,sizeof(vis));    memset(pre,-1,sizeof(pre));    int i;    while(q.size())        q.pop();    vis[sp]=1;    q.push(sp);    flow[sp]=inf;    int u;    while(q.size()&&pre[sc]==-1)    {        u = q.front();        q.pop();        for(i=0;i<n;i++)        {            if(!vis[i]&&p[u][i].c>0)            {                flow[i]=min(p[u][i].c,flow[u]);                vis[i]=1;                pre[i]=u;                q.push(i);            }        }    }    return ;}void update_maxflow(){    int s=sc;    maxflow+=augment;    while(pre[s]!=-1)    {        p[pre[s]][s].c-=augment;        p[s][pre[s]].c+=augment;        s=pre[s];    }    return ;}void max_flow(){    int i;    maxflow=0;    while(1)    {        bfs();        if(pre[sc]==-1)        {            break;        }        augment=flow[sc];        update_maxflow();    }     return ;}int main(){    std::ios::sync_with_stdio(false);    int x,y,z;    int np,cp,m;    char ch;    while(cin>>n>>np>>cp>>m)    {        int i,j;        memset(p,0,sizeof(p));        sp=n;        sc=n+1;        n+=2;        char ch;        for(i=0;i<m;i++)        {            cin>>ch>>x>>ch>>y>>ch>>z;            p[x][y].c+=z;        }        for(i=0;i<np;i++)        {            cin>>ch>>x>>ch>>y;            p[sp][x].c+=y;        }        for(i=0;i<cp;i++)        {            cin>>ch>>x>>ch>>y;            p[x][sc].c+=y;        }        max_flow();        cout<<maxflow<<endl;    }}


0 0
原创粉丝点击