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 }

时间: 2024-10-19 21:10:33

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

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there

hdu 1269 迷宫城堡(Targin算法)

---恢复内容开始--- 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Swwubmission(s): 10884    Accepted Submission(s): 4878 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单

HDU 3294 Girls&#39; research (Manacher算法 + 记录区间)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 题目大意:输入一个字符ch和一个字符串,问如果把ch当作'a'的话,字符串的每个字符也要做相应变化,如b aa,若b为'a',则b前面的a就为'a'前面的'z',这里是循环表示,输出字符串的最长回文子串,如果最长回文子串串长为1,输出No solution! 几乎是模板题,唯一的特别之处就是要输出回文串字符,所以要记录max(Mp[i])对应的在原串中的字符区间,根据Manacher算法的步骤

hdu 1533 Going Home (KM算法)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2715    Accepted Submission(s): 1366 Problem Description On a grid map there are n little men and n houses. In each unit time, every

谱聚类算法及其代码(Spectral Clustering)

简介 文章将介绍谱聚类(spectral clustering)的基本算法,以及在matlab下的代码实现.介绍内容将包括: 从图分割角度直观理解谱聚类 谱聚类算法步骤 数据以及实现代码 本文将不会涉及细节化的证明和推导,如有兴趣可参考july大神的文章从拉普拉斯矩阵说到谱聚类. 对谱聚类的理解 这一节将从图分割聚类的角度直观理解谱聚类.不过,因为本人是从事社交媒体分析的,将从一种社会关系网络的角度来介绍网络图分割成多个子图的概念. 图的分割 首先将社会关系网络看成是一个整体,每一个个体(use

HDU 1269 强连通分量tarjan算法

迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6655    Accepted Submission(s): 2973 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房

hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7062    Accepted Submission(s): 2301 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU 4911 Inversion(基本算法-排序)

Inversion Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent numbers for no more than k times. Find the minimum number of inversions after his swaps. Note: The number of inversions is the number of pair (i,j) where

HDU 4022 Bombing(基本算法-水题)

Bombing Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the opponent's base. It seems not to be a hard work in circumstances of street battles, however, you'll be encou