[状压dp]HDU3001 Travelling
题意: 走n个城市, m条路, 起点任意, 每个城市走不超过两次, 求最小花费, 不能走输出-1.
$1\le n\le 10$
分析: 每个城市的拜访次数为0 1 2, 所以三进制状压, 先预处理10位(n最大为10)的三进制数
int num[], vis[][]; void init()
{
num[]=;
for(int i=; i<=; i++)
num[i]=num[i-]*;
memset(vis, -, sizeof(vis));
for(int i=; i<=num[]; i++)
{
int x=i;
for(int j=; j<=; j++)
{
vis[i][j]=x%;
x/=;
}
}
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
//inline void print(LL x){printf(LLD, x);puts("");}
//inline void read(int &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}} #define INF 2139062143
int dp[][];
int mp[][];
int num[], vis[][];
void init()
{
num[]=;
for(int i=; i<=; i++)
num[i]=num[i-]*;
memset(vis, -, sizeof(vis));
for(int i=; i<=num[]; i++)
{
int x=i;
for(int j=; j<=; j++)
{
vis[i][j]=x%;
x/=;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
int n, m;
while(~scanf("%d%d", &n, &m))
{
memset(dp, , sizeof(dp));
memset(mp, , sizeof(mp));
for(int i=; i<n; i++)
dp[num[i]][i]=;
while(m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
mp[a-][b-]=mp[b-][a-]=min(mp[a-][b-], c);
}
int ans=INF;
for(int i=;i<num[n];i++) // n个城市走过次数的状态i
{
bool flag=;
for(int j=;j<n;j++) // 当前在j
{
if(!vis[i][j])
flag=;
if(dp[i][j]==INF)
continue;
for(int k=;k<n;k++) // 走去 k
if(j!=k)
{
if(vis[i][k]>=)
continue;
if(mp[j][k]==INF)
continue;
dp[i+num[k]][k]=min(dp[i+num[k]][k], dp[i][j]+mp[j][k]);
}
}
if(!flag)
for(int j=;j<n;j++)
ans=min(ans, dp[i][j]);
}
if(ans==INF)
printf("-1\n");
else
printf("%d\n", ans);
}
return ;
}
HDOJ3001
[状压dp]HDU3001 Travelling的更多相关文章
- 【状压dp】Travelling
[hdu3001]Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Travelling(HDU3001+状压dp+三进制+最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目: 题意:n个城市,m条边,每条边都有一个权值,问你经过所有的城市且每条边通过次数不超过两次 ...
- hdu 3001 Travelling 经过所有点(最多两次)的最短路径 三进制状压dp
题目链接 题意 给定一个\(N\)个点的无向图,求从任意一个点出发,经过所有点的最短路径长度(每个点至多可以经过两次). 思路 状态表示.转移及大体思路 与 poj 3311 Hie with the ...
- HDU 3001 Travelling (状压DP,3进制)
题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...
- HDU 3001 Travelling ——状压DP
[题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...
- HDU-4856 Tunnels (BFS+状压DP)
Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
随机推荐
- dede版权信息修改
login:dede-templets-login.htm 系统主页:dede-templets-index2.htm 主体内容在index_body.htm文件 干掉: $(function() ...
- Java多线程并发
http://wangjianwei866.blog.163.com/blog/static/9295823201231665319314/ 基于以上网文,调整了一下格式,修改了一些标点和拼写错误. ...
- 12天学好C语言——记录我的C语言学习之路(Day 5)
12天学好C语言--记录我的C语言学习之路 Day 5: 第五天的学习开始了,今天我们主要对几个程序进行编写,让自己充分的熟练编程语言,大量的题目会让自己变的精炼.以一个程序(program 5.1) ...
- C# 高精度减法 支持小数(待优化)
是现实思路 1,先小数点补位,8913758923475893274958738945793845-4893127498372459823745324532453245.284929384729837 ...
- mongodb 数据备份,还原笔记
公司数据库迁移,所以补充了一下知识: 1 集合的导入和导出 命令行帮助 mongoexport --help 导出 导出 newsServer 数据库下 news 集合 mongoexport - ...
- HTML Music Entities/音乐符号
HTML Music Entities Musical symbols Description Character(click) HTML-Entity Code-Decimal Code-Hex Q ...
- Weui 微信网站开发样式插件使用教程
微信的网页样式正式发布了,搜了一下,正式引入了乐学一百微信端的项目中. <div class="weui_grids"> <a href="javasc ...
- 手机的touch事件(基于jquery)
javascript代码: $.swipe=function(opt){ var o = $.extend({ mainSelector:"", swipeLe ...
- 笨方法学python 33课
今天Eiffel看到了第33章,任务是把一个while循环改成一个函数. 我在把while循环改成函数上很顺利,但是不知道怎么写python的主函数,在参数的调用上也出现了问题. 通过查资料,发现py ...
- Python守护进程(多线程开发)
本段代码主要作用是httpsqs队列的消费端守护进程,从httpsqs中取出数据,放入mongodb #!/usr/bin/python import sys,time,json,logging im ...