UVA - 10296 Jogging Trails (中国邮路问题)
来源:互联网 发布:防止wifi蹭网软件 编辑:程序博客网 时间:2024/05/24 15:37
Description
Problem B: Jogging Trails
Gord is training for a marathon. Behind his house is a park with a large network of jogging trails connecting water stations. Gord wants to find the shortest jogging route that travels along every trail at least once.Input consists of several test cases. The first line of input for each case contains two positive integers:n <= 15, the number of water stations, and m < 1000, the number of trails. For each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 andn, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. There may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Gord's route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case.
For each case, there should be one line of output giving the length of Gord's jogging route.
Sample Input
4 51 2 32 3 43 4 51 4 101 3 120
Output for Sample Input
41中国邮路问题(Chinese Postman Problem)是一个非常经典的图论问题:一个邮递员送信,要走完他负责投递的全部街道(所有街道都是双向通行的且每条街道可以经过不止一次),
完成任务后回到邮局,应按怎样的路线走,他所走的路程才会最短呢?如果将这个问题抽象成图论的语言,就是给定一个连通图,每条边的权值就是街道的长度,本问题转化为在图中求
一条回路,使得回路的总权值最小。
如果街道的连通图为欧拉图,则只要求出图中的一条欧拉回路即可。否则,邮递员要完成任务就必须在某些街道上重复走若干次。如果重复走一次,就加一条平行边,于是原来对应的图形
就变成了多重图。只是要求加进的平行边的总权值最小就行了。于是,我们的问题就转化为,在一个有奇度数结点的赋权连通图中,增加一些平行边,使得新图不含奇度数结点,并且
增加的边的总权值最小。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 18;const int inf = 100;int dis[maxn][maxn], odd[maxn];int dp[1<<maxn], deg[maxn];int N, M, p;int cal(int status) {if (status == 0)return 0;if (dp[status] != -1)return dp[status];int ans = 0x3f3f3f3f;for (int i = 0; i < p; i++) {if (status & (1<<i)) {for (int j = i+1; j < p; j++)if (status & (1<<j)) {int tmp = cal(status-(1<<i)-(1<<j));ans = min(ans, tmp+dis[odd[i]][odd[j]]);}//break; //notice,加与不加效率有差}}return dp[status] = ans;}int main() {while(scanf("%d%d", &N, &M) != EOF && N) {memset(dis, 63, sizeof(dis));memset(dp, -1, sizeof(dp));memset(deg, 0, sizeof(deg));int sum = 0;while (M--) {int x, y, w;scanf("%d%d%d", &x, &y, &w);dis[x][y] = min(dis[x][y], w);dis[y][x] = min(dis[y][x], w);deg[x]++, deg[y]++;sum += w;}for (int k = 1; k <= N; k++)for (int i = 1; i <= N; i++)for (int j = 1; j <= N; j++)dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);int cnt = 0;for(int i = 1; i <= N; i++)if(deg[i] % 2) odd[cnt++]=i;p = cnt;printf("%d\n", sum + cal((1<<cnt)-1));}return 0;}
- uva 10296 - Jogging Trails (中国邮路问题 状压dp)
- UVA - 10296 Jogging Trails (中国邮路问题)
- [中国邮路问题 欧拉回路 最短路 一般图匹配||状压DP] FZU 1009 Jogging Trails
- POJ-2404 Jogging Trails (中国邮递员)
- POJ 2404 Jogging Trails (中国邮递员问题,状态压缩DP)
- 中国邮路问题
- 中国邮路问题 ZOJ1903
- lightoj1086 - Jogging Trails(状态压缩dp)
- JOJ1871:Jogging Trails
- FZU1009 Jogging Trails
- POJ 2404 Jogging Trails
- lightoj1086 Jogging Trails
- POJ2404:Jogging Trails
- 1086 - Jogging Trails (欧拉回路+dp)
- LightOJ - 1086 Jogging Trails(欧拉+状态压缩)
- Light OJ 1086 Jogging Trails (Floyd+状压DP)
- Cards Ext与中国邮路问题
- POJ 2404 Jogging Trails 笔记
- .net Lock(this),Lock(obj),Lock("string") console app demo,线程安全
- 随笔分类 - NoSql
- 华为-翻译电话号码
- 澳洲联储中午将首先打响本周五国利率决议第一枪
- 动词的使役
- UVA - 10296 Jogging Trails (中国邮路问题)
- 在vs中导入项目
- Webkit学习 --- 解析HTML获取网页子资源流程
- 自己动手写CPU之第七阶段(11)——除法指令实现过程2
- cocos2d-js研究:cocos工具生成的项目移植到cocos code ide 中
- C++ Template Class Derived
- thinkphp模板里面判断一个变量是否存在于一个数组中 相当于in_array() in 或range 标签
- java二叉查找树
- outlook 2010 邮件模板使用