UVA, 820 Internet Bandwidth

最大流,注意是无向图,水题。


#include <iostream>
#include<vector>
#include<cstring>
using namespace std;
#define N 105
#define INF 1000000
struct edge
{
    int to,cap,rev;
    edge(int a,int b,int c)
    {
        to=a;
        cap=b;
        rev=c;
    }
};
vector<edge>v[N];
void add_edge(int from,int to,int cap);
int dfs(int a,int t,int f);
int max_flow(int s,int t);
int used[N];
int main()
{
    int n;
    int tt=1;
    while(cin>>n&&n)
    {
        for(int i=0;i<=n;i++)
            v[i].clear();
        int s,t,c;
        cin>>s>>t>>c;
        int a,b,d;
        for(int i=1;i<=c;i++)
        {
            cin>>a>>b>>d;
            add_edge(a,b,d);
            add_edge(b,a,d);
        }
        int ans=max_flow(s,t);
        cout<<"Network "<<tt++<<"\nThe bandwidth is "<<ans<<".\n\n";
    }
    return 0;
}
void add_edge(int from,int to,int cap)
{
    v[from].push_back(edge(to,cap,v[to].size()));
    v[to].push_back(edge(from,0,v[from].size()-1));
}
int dfs(int a,int t,int f)
{
    if(a==t)
        return f;
    used[a]=1;
    for(int i=0;i<v[a].size();i++)
    {
        edge &e=v[a][i];
        if(!used[e.to]&&e.cap>0)
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                v[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    while(1)
    {
        memset(used,0,sizeof(used));
        int f=dfs(s,t,INF);
        if(f==0)
            return flow;
        flow+=f;
    }
}




你可能感兴趣的:(internet,internet,internet,ban,band,bandwidth,UVA-820,UVA820,UVA820)