Jogging Trails
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2122   Accepted: 849

Description

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

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 and n, 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.

Output

For each case, there should be one line of output giving the length of Gord's jogging route.

Sample Input

4 5
1 2 3
2 3 4
3 4 5
1 4 10
1 3 12
0

Sample Output

41

Source

Waterloo local 2002.07.01





看的解题报告才懂得,看到有人说KM也能解决,实际实验了一下是不能够的,错误的原因在于我们拆点后,肯定会有对称边,我们希望最大匹配的边也是存在两两对称的,这样最后的结果除以2就能够了,可实际是匹配的边他可能不是对称的,导致了错误



http://www.cnblogs.com/wuminye/archive/2013/05/06/3063902.html

这人写的博客很好,能够借鉴一下

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <queue>
#define N 20
#define M 1000000
#define INF 0x7fffff
using namespace std;
int a[N][N],d[N],dis[M];
bool inque[M];
int n,m;
int main()
{
//freopen("data.txt","r",stdin);
int bfs(int x);
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
scanf("%d",&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j] = INF;
}
}
int res = 0;
memset(d,0,sizeof(d));
for(int i=1;i<=m;i++)
{
int x,y,val;
scanf("%d %d %d",&x,&y,&val);
d[x]++;
d[y]++;
res+=val;
a[x][y] = min(a[x][y],val);
a[y][x] = min(a[y][x],val);
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==k||i==j||k==j)
{
continue;
}
a[i][j] = min(a[i][j],a[i][k]+a[k][j]);
}
}
}
int sum=0,db=1;
for(int i=1;i<=n;i++)
{
if(d[i]%2)
{
sum+=db;
}
db = db*2;
}
int ans = bfs(sum);
printf("%d\n",ans+res);
}
return 0;
}
int bfs(int x)
{
memset(inque,false,sizeof(inque));
for(int i=0;i<=((1<<n)-1);i++)
{
dis[i] = INF;
}
queue<int>que;
que.push(x);
inque[x] = true;
dis[x] = 0;
int op[20],op2[20];
op2[0] = 1;
for(int i=1;i<=n;i++)
{
op2[i] = op2[i-1]*2;
}
while(!que.empty())
{
x = que.front();
que.pop();
inque[x] = false;
int xx = x;
for(int i=1;i<=n;i++)
{
op[i] = xx%2;
xx = xx/2;
}
for(int i=1;i<=n;i++)
{
if(op[i])
{
for(int j=i+1;j<=n;j++)
{
if(op[j])
{
int y =x-op2[i-1]-op2[j-1];
if(dis[y]>dis[x]+a[i][j])
{
dis[y] = dis[x]+a[i][j];
if(!inque[y])
{
que.push(y);
inque[y] = true;
}
}
}
}
}
}
}
return dis[0];
}

POJ 2404 Jogging Trails的更多相关文章

  1. POJ 2404 Jogging Trails(最小权完美匹配)

    [题目链接] http://poj.org/problem?id=2404 [题目大意] 给出一张图,求走遍所有的路径至少一次,并且回到出发点所需要走的最短路程 [题解] 如果图中所有点为偶点,那么一 ...

  2. POJ 2404 Jogging Trails [DP 状压 一般图最小权完美匹配]

    传送门 题意:找一个经过所有边权值最小的回路,$n \le 15$ 所有点度数为偶则存在欧拉回路,直接输出权值和 否则考虑度数为奇的点,连着奇数条边,奇点之间走已经走过的路移动再走没走过的路 然后大体 ...

  3. [POJ2404]Jogging Trails

    我太弱了. 我们可以知道一个结论就是对于一个图的话假如所有点的度数都是偶数,那么只需要走一波欧拉回路. 所以我们就把奇点补成偶点. 将两个奇点补充到偶点的最佳方法是选择任意两个奇点连最短路径为权的边 ...

  4. [POJ2404]Jogging Trails(中国旅行商问题)(一般图的匹配——状压DP)

    题目:http://poj.org/problem?id=2404 题意:有个n(n<=15)的点和m条无向边,每条边都有自己的权值.现在你要从某个点出发,每条边可以经过多次但要保证每条边至少走 ...

  5. [UVa10296]Jogging Trails

    题目大意: 中国邮递员问题. 给你一个无向带权连通图,求经过所有边并返回起点的最短路径. 思路: Edmonds-Johnson算法. 显然,当原图为欧拉图时,答案即为其欧拉回路的长度. 考虑原图不存 ...

  6. LightOJ1086 Jogging Trails(欧拉回路+中国邮递员问题+SPFA)

    题目求从某点出发回到该点经过所有边至少一次的最短行程. 这个问题我在<图论算法理论.实现及应用>中看过,是一个经典的问题——中国邮递员问题(CPP, chinese postman pro ...

  7. poj 2404 中国邮递员问题 欧拉回路判定+状压dp

    /* 状压dp 邮递员问题:求经过任意点出发经过每一条边一次并回到原点. 解法:1.如果是欧拉回路那么就是所有的边的总和. 2.一般的解法,找出所有的奇度顶点,任意两个顶点匹配,即最小完美匹配,可用状 ...

  8. lightoj 1086 - Jogging Trails(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1086 题解:题目就是求欧拉回路然后怎么判断有欧拉回路只要所有点的度数为偶数.那 ...

  9. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

随机推荐

  1. 第二节 EAN 8 码 / EAN 13 码

    EAN码的全名为欧洲商品条码(European Article Number),源於西元1977年,由欧洲十二个工业国家所共同发展出来的一种条码.目前已成为一种国际性的条码系统.EAN条码系统的管理是 ...

  2. Ruby学习: 类变量和类方法

    一.类变量 在ruby中,可以为类定义类变量,类变量的值为类的所有实例(对象)所共享. 有点类似其它语言(如java)中的静态变量,但与java中的静态变量的区别是, 类变量是私有的,无法在类的外部访 ...

  3. Maven2的配置文件settings.xml(转)

    http://maven.apache.org/settings.html简介: 概览当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使 ...

  4. viminfo: 无效的启动字符

    当自己进入一个用户,使用vi打开一个文件时,出现以下情况: [gexd@localhost ~]$ vi test.c E575: viminfo: 无效的启动字符 位于行: int main() . ...

  5. poj3308Paratroopers(最小割)

    题目请戳这里 题目大意:给一个n*m的矩阵,给一些点(ri,ci)表示该点在第ri行第ci列.现在要覆盖所有的点,已知覆盖第i行代价为Ri,覆盖第j列代价为Cj.总代价是累乘的,求最小总代价能覆盖所有 ...

  6. java学习之IO对象流

    //注意对象类要打标记实现Serializable接口 package com.gh; import java.io.FileInputStream; import java.io.FileNotFo ...

  7. poj 3281 Dining 网络流-最大流-建图的题

    题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...

  8. centos下彻底删除MYSQL 和重新安装MYSQL

    在Centos6.3上装了一个Mysql,结果mysql库被我玩丢了(这里面管理了mysql的权限).现在采用先彻底删除,然后重新安装Mysql. 1 删除Mysql yum remove  mysq ...

  9. Word中使用代码高亮插件

    Word中使用代码高亮插件 1.下载并安装:SyntaxHighlighter4Word.zip 解压,然后双击bin\word2010\Kong.SyntaxHighlighter.Word2010 ...

  10. php定时输出

    //PHP定时输出 ob_end_flush(); //关闭输出缓冲 set_time_limit(0); //设置最大执行时间为无限制 echo '============开始=========== ...