hdu 1797 靠谱的算法应该是最大生成树,但是本人用最大流做的

Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 22294   Accepted: 5916

Description

Background 
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1

3 3

1 2 3

1 3 4

2 3 5

Sample Output

Scenario #1:

4

Source

 

附上代码:

  1 #include<iostream>

  2 #include<queue>

  3 #include<cstring>

  4 #include<cstdio>

  5 #include<climits>

  6 #define MAXE 1010*1010*2

  7 #define MAXP 1010

  8 #define Max(a,b) a>b?a:b

  9 #define Min(a,b) a<b?a:b

 10 using namespace std;

 11 struct Edge

 12 {

 13     int s,t,f,next;

 14 } edge[MAXE];

 15 int head[MAXP];

 16 int cur[MAXP];

 17 int pre[MAXP];

 18 int stack[MAXE];

 19 int used[MAXP];

 20 int ent;

 21 int maxn;

 22 int n,m,s,t;

 23 int num;

 24 void add(int start,int last,int f)

 25 {

 26     edge[ent].s=start;

 27     edge[ent].t=last;

 28     edge[ent].f=f;

 29     edge[ent].next=head[start];

 30     head[start]=ent++;

 31     edge[ent].s=last;

 32     edge[ent].t=start;

 33     edge[ent].f=0;

 34     edge[ent].next=head[last];

 35     head[last]=ent++;

 36 }

 37 bool bfs(int S,int T)

 38 {

 39     memset(pre,-1,sizeof(pre));

 40     pre[S]=0;

 41     queue<int>q;

 42     q.push(S);

 43     while(!q.empty())

 44     {

 45         int temp=q.front();

 46         q.pop();

 47         for(int i=head[temp]; i!=-1; i=edge[i].next)

 48         {

 49             int temp2=edge[i].t;

 50             if(pre[temp2]==-1&&edge[i].f>maxn)

 51             {

 52                 pre[temp2]=pre[temp]+1;

 53                 q.push(temp2);

 54             }

 55         }

 56     }

 57     return pre[T]!=-1;

 58 }

 59 void dinic(int start,int last)

 60 {

 61     int flow=0,now;

 62     maxn=0;

 63     while(bfs(start,last))

 64     {

 65         int top=0;

 66         memcpy(cur,head,sizeof(head));

 67         int u=start;

 68         while(1)

 69         {

 70             if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流

 71             {

 72                 int minn=INT_MAX;

 73                 for(int i=0; i<top; i++)

 74                 {

 75                     if(minn>edge[stack[i]].f)

 76                     {

 77                         minn=edge[stack[i]].f;

 78                         now=i;

 79                     }

 80                 }

 81                 maxn=Max(maxn,minn);

 82                 edge[stack[now]].f=edge[stack[now]^1].f=0;

 83                 top=now;

 84                 u=edge[stack[top]].s;

 85             }

 86             for(int i=cur[u]; i!=-1; cur[u]=i=edge[i].next) //找出从u点出发能到的边

 87                 if(edge[i].f&&pre[edge[i].t]==pre[u]+1)

 88                     break;

 89             if(cur[u]==-1)//如果从该点未找到可行边,将该点标记并回溯

 90             {

 91                 if(top==0)break;

 92                 pre[u]=-1;

 93                 u=edge[stack[--top]].s;

 94             }

 95             else//如果找到了继续运行

 96             {

 97                 stack[top++]=cur[u];

 98                 u=edge[cur[u]].t;

 99             }

100         }

101     }

102 }

103 int main()

104 {

105     int cas;

106     cin>>cas;

107     int sum=1;

108     while(cas--)

109     {

110         memset(head,-1,sizeof(head));

111         ent=0;

112         scanf("%d%d",&n,&m);

113         s=1;t=n;

114         int u,v,flow;

115         for(int i=0;i<m;i++)

116         {

117             scanf("%d%d%d",&u,&v,&flow);

118             add(u,v,flow);

119             add(v,u,flow);

120         }

121         printf("Scenario #%d:\n",sum++);

122         dinic(s,t);

123         printf("%d\n\n",maxn);

124     }

125     return 0;

126 }
View Code

 

你可能感兴趣的:(HDU)