博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1853 Cyclic Tour(费用流OR二分图最佳匹配,5级)
阅读量:4576 次
发布时间:2019-06-08

本文共 2541 字,大约阅读时间需要 8 分钟。

O - Cyclic Tour
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u
 
Appoint description:  System Crawler  (2013-05-30)

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).
 

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 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1
 

Sample Output

 
42 -1

Hint

In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
 
#include
#include
#include
#include
#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=234;const int oo=1e9;bool vis[mm];int head[mm],edge,work[mm],dis[mm],src,dest,node;class Edge{ public:int v,next,flow,cost;}e[mm*mm*2];void data(){ clr(head,-1);edge=0;}void add(int u,int v,int _flow,int _cost){ e[edge].v=v;e[edge].flow=_flow;e[edge].cost=_cost;e[edge].next=head[u];head[u]=edge++; e[edge].v=u;e[edge].flow=0;e[edge].cost=-_cost;e[edge].next=head[v];head[v]=edge++;}bool spfa(){ FOR(i,0,node)dis[i]=oo,vis[i]=0; queue
Q;work[src]=work[dest]=-1; Q.push(src);int u,v;dis[src]=0; while(!Q.empty()) { u=Q.front();Q.pop();vis[u]=0; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(e[i].flow&&dis[v]>dis[u]+e[i].cost) { dis[v]=dis[u]+e[i].cost;work[v]=i^1;//path if(vis[v])continue;vis[v]=1;Q.push(v); } } } //FOR(i,0,node)printf("d=%d %d\n",i,dis[i]); return work[dest]!=-1;}int max_spfa(int& many){ int ret=0,num; while(spfa()) { num=oo;//puts("++"); for(int i=work[dest];~i;i=work[e[i].v]) { if(e[i^1].flow

转载于:https://www.cnblogs.com/nealgavin/p/3797618.html

你可能感兴趣的文章
公共POI导出Excel方法–java
查看>>
次短路——Dijkstra
查看>>
Enter Query Mode Search Tricks Using Enter_Query Built-in in Oracle Forms
查看>>
Form属性、内置子程序、触发器、系统变量
查看>>
广州夜景一
查看>>
linux编译安装python3和安装django
查看>>
PHP数组对象互转
查看>>
JVM(2)--一文读懂垃圾回收
查看>>
iOS ERROR: unable to get the receiver data from the DB 解决方式
查看>>
游戏开发——战斗系统设计技巧
查看>>
Android ROM 制作教程
查看>>
Android模拟器使用SD卡
查看>>
新手Oracle安装及使用入门
查看>>
帝国cms灵动标签下常用标签
查看>>
STL学习笔记(关联式容器)
查看>>
Android生成xml
查看>>
python入到到实战--第十章----文件
查看>>
FMDataBase 打开sqlite的外键约束功能
查看>>
Nmap 7.70新增功能——扫描主机所有IP
查看>>
二分图
查看>>