HDU 3488 2010多校联合6 图论
来源:互联网 发布:淘宝店铺高级装修 编辑:程序博客网 时间:2024/06/03 17:08
Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 725 Accepted Submission(s): 363
Problem Description
In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
Input
An integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
Output
For each test case, output a line with exactly one integer, which is the minimum total distance.
Sample Input
16 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 4
Sample Output
42
Source
2010 ACM-ICPC Multi-University Training Contest(6)——Host by BIT
刚开始看题表示完全木有思路。
后来看到别人用匹配。然后想了下,觉得有道理,就写了下,还好,1Y。
不过个人感觉还是没有领悟到精髓所在。。恩
先把代码贴出来,在仔细想想。恩
我的代码:
#include<stdio.h>#include<string.h>#define inf 99999999int map[205][205];int lx[205],ly[205];bool x[205],y[205];int link[205];int n;bool dfs(int u){int i;x[u]=true;for(i=1;i<=n;i++){if(lx[u]+ly[i]==map[u][i]&&!y[i]){y[i]=true;if(link[i]==-1||dfs(link[i])){link[i]=u;return true;}}}return false;}int main(){int i,t,j,m;int a,b,l,k;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=1;i<=n;i++)for(j=1;j<=n;j++)map[i][j]=-inf;for(i=1;i<=m;i++){scanf("%d%d%d",&a,&b,&l);if(map[a][b]<-l)map[a][b]=-l;}memset(x,0,sizeof(x));memset(y,0,sizeof(y));memset(link,-1,sizeof(link));for(i=0;i<205;i++)lx[i]=inf;memset(ly,0,sizeof(ly));for(k=1;k<=n;k++){while(1){memset(x,0,sizeof(x));memset(y,0,sizeof(y));if(dfs(k))break;int d=inf;for(i=1;i<=n;i++)if(x[i])for(j=1;j<=n;j++)if(!y[j]&&lx[i]+ly[j]-map[i][j]<d)d=lx[i]+ly[j]-map[i][j];for(i=1;i<=n;i++)if(x[i])lx[i]=lx[i]-d;for(i=1;i<=n;i++)if(y[i])ly[i]=ly[i]+d;}}int ans=0;for(i=1;i<=n;i++)ans=ans+map[link[i]][i];printf("%d\n",-ans);}return 0;}
- HDU 3488 2010多校联合6 图论
- HDU 3478 2010多校联合5 图论
- HDU 3521 2010多校联合第9场 数学
- 多校联合练习赛6 HDU 4655 Cut Pierces
- 13hdu多校联合练习#1
- hdu多校联合Peter's Hobby
- hdu 5319(多校联合赛)
- HDU 5761 多校联合 Rower BO
- 多校联合训练4&&HDU 5763
- 多校联合训练7&&HDU 5810
- 多校联合训练8&&HDU 5828
- HDU 5317 RGCDQ(2015多校联合)
- 2014多校联合六(HDU 4923 HDU 4925 HDU 4927 HDU 4930)
- 2014多校联合七(HDU 4937 HDU 4938 HDU 4939 HDU 4941)
- 2014多校联合十(HDU 4972 HDU 4973 HDU 4974 HDU 4975)
- HDU/HDOJ 3519 矩阵二分幂 2010多校联合第九场
- HDU/HDOJ 3609 Up-up 2010多校联合17场ZSTU
- HDU 4655 2013多校联合赛第6场 Cut Pieces
- 无题(1)
- CSS3动画之二:Animations功能
- Linux文件系统--udhcpc动态获取ip配置
- 方法论之 如何解决一个问题
- 多处理器编程的艺术 第3章 并发对象
- HDU 3488 2010多校联合6 图论
- 利用Spring 和DBCP实现分布式动态数据库连接池管理
- java常用类解析六:IO系统文件读写工具类
- 架构师给程序员的一封信
- 大中型网站网站(B/S开发中) cache应用(多图)
- C# 在windows form实现不规则窗体&透明窗体
- 多态性
- 编程技术面试的五大要点
- umask-文件权限的补码