POJ 1273(HDU 1532) Drainage Ditches|HDU 3549 Flow Problem|很典型&&裸的網絡流

看我異次元傳送門

border="0" width="330" height="86" src="http://music.163.com/outchain/player?type=2&id=28377210&auto=1&height=66">

**

Drainage Ditches

**
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 64219 Accepted: 24777


**

Description

**

Every time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie’s clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.


**

Input

**

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.


**

Output

**

For each case, output a single integer, the maximum rate at which water may emptied from the pond.


**

Sample Input

**

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10


**

Sample Output

**

50
**

Source

**

USACO 93


簡單題意

單向邊的最大流 1 是起點 m是終點


**

Solution

**
起初Wa==》發現多組數據…

然後TLE 發現maxn定義等於200…

估計是被NOIP自己的成績嚇哭了…

裸題不多說…代碼覺得還是不是很長的..QAQ

用的鄰接表儲存的 如果希望看其他版本的代碼 點這裏這裏寫鏈接內容

**

Code

**

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn=205;

struct data{int to,next,w;}e[maxn*maxn];

int head[maxn],cnt=1,h[maxn],n,m,ans;

void ins(int u,int v,int w){cnt++;e[cnt].to=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;}

bool bfs(){
    memset(h,-1,sizeof(h));
    h[1]=0;
    queue<int> q;
    q.push(1);
    while(!q.empty())
    {
        int x=q.front();q.pop();
        for(int i=head[x];i;i=e[i].next)
            if(e[i].w&&h[e[i].to]<0)
            {
                q.push(e[i].to);
                h[e[i].to]=h[x]+1;
            }
    }
    if(h[n]==-1)return 0;
    return 1;
}
int dfs(int x,int f){
    if(x==n)return f;
    int w,used=0;
    for(int i=head[x];i;i=e[i].next)
    if(e[i].w&&h[e[i].to]==h[x]+1)
    {
        w=f-used;
        w=dfs(e[i].to,min(w,e[i].w));
        e[i].w-=w;
        e[i^1].w+=w;
        used+=w;
        if(used==f)return f;
    }
    if(!used)h[x]=-1;
    return used;
}
void Dinic(){
    while(bfs()){

        ans+=dfs(1,2000000000);
    }
}
int main()
{

    while(scanf("%d%d",&m,&n)==2)
    {
        ans=0;cnt=1;
        memset(head,0,sizeof(head));
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        ins(u,v,w);
        ins(v,u,0);
    }
    Dinic();
    printf("%d\n",ans); 
    }
    return 0;
}



**

HDU 3549

**

這是後來加的…… 一樣……就改了改細節……

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn=205;

struct data{int to,next,w;}e[maxn*maxn];

int head[maxn],cnt=1,h[maxn],n,m,ans,T;

void ins(int u,int v,int w){cnt++;e[cnt].to=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;}

bool bfs(){
    memset(h,-1,sizeof(h));
    h[1]=0;
    queue<int> q;
    q.push(1);
    while(!q.empty())
    {
        int x=q.front();q.pop();
        for(int i=head[x];i;i=e[i].next)
            if(e[i].w&&h[e[i].to]<0)
            {
                q.push(e[i].to);
                h[e[i].to]=h[x]+1;
            }
    }
    if(h[n]==-1)return 0;
    return 1;
}
int dfs(int x,int f){
    if(x==n)return f;
    int w,used=0;
    for(int i=head[x];i;i=e[i].next)
    if(e[i].w&&h[e[i].to]==h[x]+1)
    {
        w=f-used;
        w=dfs(e[i].to,min(w,e[i].w));
        e[i].w-=w;
        e[i^1].w+=w;
        used+=w;
        if(used==f)return f;
    }
    if(!used)h[x]=-1;
    return used;
}
void Dinic(){
    while(bfs()){

        ans+=dfs(1,2000000000);
    }
}
int main()
{
    int T,Cnt=0;
    scanf("%d",&T);
    while(T--&&scanf("%d%d",&n,&m)==2)
    {
        Cnt++;
        ans=0;cnt=1;
        memset(head,0,sizeof(head));
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        ins(u,v,w);
        ins(v,u,0);
    }
    Dinic();
    printf("Case %d: %d\n",Cnt,ans);
    }
    return 0;
}


——既然選擇了遠方,便只顧風雨兼程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章