POJ 2404 Jogging Trails
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2122 | Accepted: 849 |
Description
Input
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
Sample Input
4 5
1 2 3
2 3 4
3 4 5
1 4 10
1 3 12
0
Sample Output
41
Source
看的解题报告才懂得,看到有人说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的更多相关文章
- POJ 2404 Jogging Trails(最小权完美匹配)
[题目链接] http://poj.org/problem?id=2404 [题目大意] 给出一张图,求走遍所有的路径至少一次,并且回到出发点所需要走的最短路程 [题解] 如果图中所有点为偶点,那么一 ...
- POJ 2404 Jogging Trails [DP 状压 一般图最小权完美匹配]
传送门 题意:找一个经过所有边权值最小的回路,$n \le 15$ 所有点度数为偶则存在欧拉回路,直接输出权值和 否则考虑度数为奇的点,连着奇数条边,奇点之间走已经走过的路移动再走没走过的路 然后大体 ...
- [POJ2404]Jogging Trails
我太弱了. 我们可以知道一个结论就是对于一个图的话假如所有点的度数都是偶数,那么只需要走一波欧拉回路. 所以我们就把奇点补成偶点. 将两个奇点补充到偶点的最佳方法是选择任意两个奇点连最短路径为权的边 ...
- [POJ2404]Jogging Trails(中国旅行商问题)(一般图的匹配——状压DP)
题目:http://poj.org/problem?id=2404 题意:有个n(n<=15)的点和m条无向边,每条边都有自己的权值.现在你要从某个点出发,每条边可以经过多次但要保证每条边至少走 ...
- [UVa10296]Jogging Trails
题目大意: 中国邮递员问题. 给你一个无向带权连通图,求经过所有边并返回起点的最短路径. 思路: Edmonds-Johnson算法. 显然,当原图为欧拉图时,答案即为其欧拉回路的长度. 考虑原图不存 ...
- LightOJ1086 Jogging Trails(欧拉回路+中国邮递员问题+SPFA)
题目求从某点出发回到该点经过所有边至少一次的最短行程. 这个问题我在<图论算法理论.实现及应用>中看过,是一个经典的问题——中国邮递员问题(CPP, chinese postman pro ...
- poj 2404 中国邮递员问题 欧拉回路判定+状压dp
/* 状压dp 邮递员问题:求经过任意点出发经过每一条边一次并回到原点. 解法:1.如果是欧拉回路那么就是所有的边的总和. 2.一般的解法,找出所有的奇度顶点,任意两个顶点匹配,即最小完美匹配,可用状 ...
- lightoj 1086 - Jogging Trails(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1086 题解:题目就是求欧拉回路然后怎么判断有欧拉回路只要所有点的度数为偶数.那 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
随机推荐
- RMAN多种备份脚本分享
1.相关参数介绍: 命令行参数 描述 TARGET 为目标数据库定义的一个连接字符串,当连接到一个目标数据库时,该连续是SYSDBA连接.该用户拥有启动和关闭数据库的权利,必须属于OSDBA组,必须建 ...
- Python 中的list小结
list的下标和子list list的下表从零开始,和C语言挺类似的,但是增加了负下标的使用. -len-----第一个元素 ...... ...... -2 ------ 倒数第二个元素 ...
- 欧拉函数K - Relatives
欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n). 特殊性质:当n为奇数时,φ(2n)=φ(n), φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..( ...
- Android中的TextView实现多行显示省略号
今天遇到一个问题就是TextView显示内容的时候,多行显示的时候,显示省略号的问题,刚开始没有找到一个好的办法,只找到一个自定义TextView组件的方法,然而今天在贴吧中找到一个更好,更简便的方法 ...
- CentOS用yum安装搭建LAMP
#1.安装Apache yum install httpd httpd-devel #启动apache /etc/init.d/httpd start #设为开机启动: chkconfig httpd ...
- C++编译时函数名修饰约定规则(很具体),MFC提供的宏,extern "C"的作用
调用约定: __cdecl __fastcall与 __stdcall,三者都是调用约定(Calling convention),它决定以下内容:1)函数参数的压栈顺序,2)由调用者还是被调用者把参数 ...
- 基于visual Studio2013解决算法导论之024双向链表实现
题目 双向链表的实现 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> #i ...
- 基于visual Studio2013解决C语言竞赛题之0507筛选素数
题目
- DP HDIJ1421 搬宿舍
Problem Description 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆, ...
- 新浪微博中tableview中头部信息
摘自http://www.cnblogs.com/gcb999/p/3151665.html #import <UIKit/UIKit.h> @class User; @protocol ...