日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

HDU Problem - 4280 Island Transport(最大流)

發布時間:2024/4/18 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 4280 Island Transport(最大流) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

  In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.  You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.  The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.

Input

  The first line contains one integer T (1<=T<=20), the number of test cases.  Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.  Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.  Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.  It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.

Output

  For each test case, output an integer in one line, the transport capacity.

Sample Input

2 5 7 3 3 3 0 3 1 0 0 4 5 1 3 3 2 3 4 2 4 3 1 5 6 4 5 3 1 4 4 3 4 2 6 7 -1 -1 0 1 0 2 1 0 1 1 2 3 1 2 1 2 3 6 4 5 5 5 6 3 1 4 6 2 5 5 3 6 4

Sample Output

9 6

AC

  • 找到源點和匯點,裸的最大流
#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <algorithm> #include <cmath> #define N 100005 #include <cstring> #define ll long long #define P pair<int, int> #define mk make_pair using namespace std;int n, m, cnt; int inf = 0x3f3f3f3f; int level[N]; struct ac{int v, c, pre; }edge[N * 2]; int head[N], dis[N], curedge[N]; void init() {cnt = 0;memset(dis, -1, sizeof(dis));memset(edge, 0, sizeof(edge));memset(head, -1, sizeof(head)); } void addedge(int u, int v, int c) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].pre = head[u];head[u] = cnt++; } bool bfs(int s, int e) {queue<int> que;memset(level, 0, sizeof(level));level[s] = 1;que.push(s);while (!que.empty()) {int u = que.front();que.pop();for (int i = head[u]; i != -1; i = edge[i].pre) {if (level[edge[i].v] == 0 && edge[i].c > 0) {level[edge[i].v] = level[u] + 1;que.push(edge[i].v);}}}return level[e] != 0; } int dfs(int s, int e, int flow) {if (s == e)return flow;for (int &i = curedge[s]; i != -1; i = edge[i].pre) {if (level[edge[i].v] == level[s] + 1 && edge[i].c > 0) {int d = dfs(edge[i].v, e, min(flow, edge[i].c));if (d > 0) {edge[i].c -= d;edge[i ^ 1].c += d;return d;}}}return 0; } int Dinic(int s, int e) {int ans = 0;while (bfs(s, e)) {for (int i = 1; i <= n; ++i) {curedge[i] = head[i];}int d;while (d = dfs(s, e, inf))ans += d;}return ans; }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint t;scanf("%d", &t);while (t--) {init();scanf("%d%d", &n, &m);int l = inf, r = -inf, s, e;for (int i = 1; i <= n; ++i) {int t1, t2;scanf("%d%d", &t1, &t2);if (l > t1) l = t1, s = i;if (r < t1) r = t1, e = i;}for (int i = 0; i < m; ++i) {int u, v, c;scanf("%d%d%d", &u, &v, &c);// 雙向邊 addedge(u, v, c);addedge(v, u, c);}int ans = Dinic(s, e);printf("%d\n", ans);}return 0; }

總結

以上是生活随笔為你收集整理的HDU Problem - 4280 Island Transport(最大流)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。