HDU 1853--Cyclic Tour【最小费用最大流 && 有向环最小权值覆盖 】
来源:互联网 发布:细说php下载 编辑:程序博客网 时间:2024/05/16 09:49
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1950 Accepted Submission(s): 984
Problem Description
There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
Sample Input
6 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 46 51 2 12 3 13 4 14 5 15 6 1
Sample Output
42-1HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
水一遍熟悉熟悉模板。
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define INF 0x3f3f3f3f#define maxn 500#define maxm 77000using namespace std;int n, m;int outset;int inset;struct node { int u, v, cap, flow, cost, next;};node edge[maxm];int head[maxn], cnt;int per[maxn];//记录增广路径上 到达点i的边的编号int dist[maxn], vis[maxn];void init(){ cnt = 0; memset(head, -1, sizeof(head));}void add(int u, int v, int w, int c){ int i; for(i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(v == E.v) break; } if(i != -1){ if(edge[i].cost > c) edge[i].cost = c, edge[i ^ 1].cost = -c; return ; } //edge[cnt] = {u, v, w, 0, c, head[u]}这样写就直接超时了。 node E1 = {u, v, w, 0, c, head[u]}; edge[cnt] = E1; head[u] = cnt++; node E2 = {v, u, 0, 0, -c, head[v]}; edge[cnt] = E2; head[v] = cnt++;}void getmap(){ outset = 0; inset = n * 2 + 1; for(int i = 1; i <= n; ++i){ add(outset, i, 1, 0); add(i + n, inset, 1, 0); } while(m--){ int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b + n, 1, c); }}bool SPFA(int st, int ed){ queue<int>q; for(int i = 0; i <= inset; ++i){ dist[i] = INF; vis[i] = 0; per[i] = -1; } dist[st] = 0; vis[st] = 1; q.push(st); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(dist[E.v] > dist[u] + E.cost && E.cap > E.flow){//可以松弛 且 没有满流 dist[E.v] = dist[u] + E.cost; per[E.v] = i;//记录到达这个点的边的编号 if(!vis[E.v]){ vis[E.v] = 1; q.push(E.v); } } } } return per[ed] != -1;}void MCMF(int st, int ed, int &cost, int &flow){ flow = 0;//总流量 cost = 0;//总费用 while(SPFA(st, ed)){//每次寻找花销最小的路径 int mins = INF; for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){ mins = min(mins, edge[i].cap - edge[i].flow); } //增广 for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){ edge[i].flow += mins; edge[i ^ 1].flow -= mins; cost += edge[i].cost * mins; } flow += mins; }}int main (){ while(scanf("%d%d", &n, &m) != EOF){ init(); getmap(); int cost, flow; MCMF(outset, inset, cost, flow); if(flow == n) printf("%d\n", cost); else printf("-1\n"); } return 0;}
4 4
- HDU 1853--Cyclic Tour【最小费用最大流 && 有向环最小权值覆盖 】
- hdu 1853 Cyclic Tour (有向环最小权值覆盖,费用流)
- HDU 3488--Tour【最小费用最大流 && 有向环最小权值覆盖 && 经典】
- HDU 3488 Tour(最小费用流:有向环覆盖)
- hdu 3488 Tour (有向环最小权值覆盖,费用流)
- Hdu 1853 Cyclic Tour - 最小费用最大流
- hdu 1853 Cyclic Tour(最小费用最大流)
- hdu 1853 Cyclic Tour 最小费用最大流
- hdu 1853 Cyclic Tour 最小费用最大流
- HDU 1853 Cyclic Tour(最小费用最大流)
- hdu 1853 Cyclic Tour(最小费用最大流)
- hdu 1853 Cyclic Tour【最小费用最大流】
- hdu-1853 Cyclic Tour(最小费用最大流)
- HDU 1853 Cyclic Tour(最小费用流)
- hdu 1853 Cyclic Tour(最小权匹配/费用流)
- HDU 1853 Cyclic Tour && HDU 3488Tour 最小费用流
- HDU 1853 Cyclic Tour【最小费用最大流 OR 带权二分图匹配】
- HDU 1853 Cyclic Tour (最小费用最大流+环的判断)
- UDP主要丢包原因及具体问题分析
- Java文件列表显示
- 求序列中满足Ai < Aj > Ak and i < j < k的组数 树状数组 HIT 2275 Number sequence
- swift详解之十四 -----------NSThread 异步下载图片
- 题目:二叉查找树中搜索区间
- HDU 1853--Cyclic Tour【最小费用最大流 && 有向环最小权值覆盖 】
- ZOJ 2104解题报告
- Java集合之Map
- django form关于clean及cleaned_data的说明 以及4种初始化
- 题目:二叉树中的最大路径和
- Android屏幕适配全攻略(最权威的官方适配指导)
- POJ 3185 The Water Bowls(高斯消元法,枚举自由变元)
- 开发中遇到detached head的解决方法
- install scrapy