light oj 1153 - Internet Bandwidth【网络流无向图】
Time Limit: 2 second(s) | Memory Limit: 32 MB |
On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying capacity (bandwidth) between two given nodes is the maximal amount of data per unit time that can be transmitted from one node to the other. Using a technique called packet switching; this data can be transmitted along several paths at the same time.
For example, the following figure shows a network with four nodes (shown as circles), with a total of five connections among them. Every connection is labeled with a bandwidth that represents its data-carrying capacity per unit time.
In our example, the bandwidth between node 1 and node 4 is 25, which might be thought of as the sum of the bandwidths 10 along the path 1-2-4, 10 along the path 1-3-4, and 5 along the path 1-2-3-4. No other combination of paths between nodes 1 and 4 provides a larger bandwidth.
You must write a program that computes the bandwidth between two given nodes in a network, given the individual bandwidths of all the connections in the network. In this problem, assume that the bandwidth of a connection is always the same in both directions (which is not necessarily true in the real world).
Input
Input starts with an integer T (≤ 30), denoting the number of test cases.
Every description starts with a line containing an integer n (2 ≤ n ≤ 100), which is the number of nodes in the network. The nodes are numbered from 1 to n. The next line contains three numbers s, t, and c. The numbers s and t are the source and destination nodes, and the number c (c ≤ 5000, s ≠ t) is the total number of connections in the network. Following this are c lines describing the connections. Each of these lines contains three integers: the first two are the numbers of the connected nodes, and the third number is the bandwidth of the connection. The bandwidth is a non-negative number not greater than 1000.
There might be more than one connection between a pair of nodes, but a node cannot be connected to itself. All connections are bi-directional, i.e. data can be transmitted in both directions along a connection, but the sum of the amount of data transmitted in both directions must be less than the bandwidth.
Output
For each case of input, print the case number and the total bandwidth between the source node s and the destination node t.
Sample Input |
Output for Sample Input |
2 4 1 4 5 1 2 20 1 3 10 2 3 5 2 4 10 3 4 20 4 1 4 2 1 4 20 1 4 20 |
Case 1: 25 Case 2: 40 |
注意:此题是无向图的网络流,在处理反向边的时候,不能把反向边的容量设置为0 而要设置的和正向边一样
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- #include<math.h>
- #include<algorithm>
- #define MAX 10010
- #define INF 0x7ffff
- #define MAXM 100100
- using namespace std;
- struct node
- {
- int from,to,cap,flow,next;
- }edge[MAXM];
- int dis[MAX],vis[MAX];
- int cur[MAX];
- int ans,head[MAX];
- void init()
- {
- ans=0;
- memset(head,-1,sizeof(head));
- }
- void add(int u,int v,int w)
- {
- edge[ans]={u,v,w,0,head[u]};
- head[u]=ans++;
- edge[ans]={v,u,w,0,head[v]};//这是有向图 无向图 edge[ans]={v,u,0,0,head[v]};
- head[v]=ans++;
- }
- int start,hui,sum;
- void getmap()
- {
- scanf("%d%d%d",&start,&hui,&sum);
- for(int i=1;i<=sum;i++)
- {
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- add(a,b,c);
- }
- }
- int bfs(int beg,int end)
- {
- queue<int>q;
- memset(vis,0,sizeof(vis));
- memset(dis,-1,sizeof(dis));
- while(!q.empty()) q.pop();
- vis[beg]=1;
- dis[beg]=0;
- q.push(beg);
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- for(int i=head[u];i!=-1;i=edge[i].next)
- {
- node E=edge[i];
- if(!vis[E.to]&&E.cap>E.flow)
- {
- dis[E.to]=dis[u]+1;
- vis[E.to]=1;
- if(E.to==end) return 1;
- q.push(E.to);
- }
- }
- }
- return 0;
- }
- int dfs(int x,int a,int end)
- {
- if(x==end||a==0)
- return a;
- int flow=0,f;
- for(int& i=cur[x];i!=-1;i=edge[i].next)
- {
- node& E=edge[i];
- if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)
- {
- E.flow+=f;
- edge[i^1].flow-=f;
- flow+=f;
- a-=f;
- if(a==0) break;
- }
- }
- return flow;
- }
- int maxflow(int beg,int end)
- {
- int flow=0;
- while(bfs(beg,end))
- {
- memcpy(cur,head,sizeof(head));
- flow+=dfs(beg,INF,end);
- }
- return flow;
- }
- int main()
- {
- int t,k,n,m;
- scanf("%d",&t);
- k=1;
- while(t--)
- {
- scanf("%d",&n);
- init();
- getmap();
- printf("Case %d: %d\n",k++,maxflow(start,hui));
- }
- return 0;
- }
light oj 1153 - Internet Bandwidth【网络流无向图】的更多相关文章
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
- Jan's light oj 01--二分搜索篇
碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...
- Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...
随机推荐
- 软媒魔方u盘装系统
http://jingyan.baidu.com/article/d5a880eb6e747e13f147ccd6.html
- nutch 采集效率--设置采集间隔
fetcher.max.crawl.delay 默认是30秒,这里改为 5秒修改nutch-default.xml<property> <name>fetcher.max.cr ...
- Jmeter 使用笔记之 html 报告扩展(一)
题记:在用 loadrunner 的时候可以生成一个 HTML 的报告,并且里面包含各种图表,各种详细的数据.而在使用 Jmeter 测试完后并不能直接生成 Html 的报告(无论是用 GUI 还是命 ...
- javascript技巧合集
转http://www.blogjava.net/zhaochengming/archive/2010/04/09/317837.html http://www.cnblogs.com/fxgachi ...
- explain 用法详解
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...
- LINUX关闭防火墙(转载)
(1) 重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off (2) 即时生效,重启后失效: 开启:service iptables ...
- HTML textarea输入框限制长度 (引)
引自:http://aqingsao.iteye.com/blog/398897 textarea在Web开发中经常用到,但是它本身不支持maxlength,可以通过下面的js实现: function ...
- asp.net 框架接触(2)
1. 学习一个框架就要尽量按照它的各种规则(命名规则等)来命名,写代码 比如 下列Entity层内的代码"StudentInfo"编写应与数据库内的表名严格对应 不然就会报错 [T ...
- AD设计中,三种大面积覆铜的区别
在AD设计中,主要有三种大面积覆铜方式,分别是Fill(铜皮) Polygon Pour(灌铜)和Plane(平面层),这三种方式刚开始的时候没有细细区分,现在分别应用了一下, 总结如下,欢迎指正 F ...
- HTTP访问控制(CORS)
跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP请求.比如说,域名A(http://domaina.exampl ...