HDU 5418 Victor and World(状压DP+Floyed预处理)
Victor and World
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 1463 Accepted Submission(s): 682
Victor now is at the country whose number is 1, he wants to know the minimal amount of fuel for him to visit every country at least once and finally return to the first country.
In every test case, there are two integers n and m in the first line, denoting the number of the countries and the number of the flights.
Then there are m lines, each line contains three integers ui, vi and wi, describing a flight.
1≤T≤20.
1≤n≤16.
1≤m≤100000.
1≤wi≤100.
1≤ui,vi≤n.
题目链接:HDU 5418
状态转移方程:dp[s'][v]=min(dp[s'][v], dp[s][u]+dis[u][v]),dp[a][b]表示当前已经到达过点的状态为a,且最后到达的点是b,那么显然一开始没有到达过任何点,但一开始所在点为1,因此dp[0][0]=0(把点的标号均减去1方便运算),然后枚举每一个s中的方案,s'为s到s'所要经过的点v,距离显然就要增加上dis[u][v],其中u为s中的点,v为s'中的点。
代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 17;
int E[N][N], dp[1 << N][N]; void init()
{
CLR(E, INF);
CLR(dp, INF);
for (int i = 0; i < N; ++i)
E[i][i] = 0;
}
int main(void)
{
int tcase, n, m, i;
scanf("%d", &tcase);
while (tcase--)
{
init();
scanf("%d%d", &n, &m);
for (i = 0; i < m; ++i)
{
int a, b, w;
scanf("%d%d%d", &a, &b, &w);
--a;
--b;
E[a][b] = E[b][a] = min(E[a][b], w);
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
E[i][j] = min(E[i][j], E[i][k] + E[k][j]);
dp[0][0] = 0;
int st_cnt = 1 << n;
for (int s = 0; s < st_cnt; ++s) //已经走过的点的状态
{
for (int u = 0; u < n; ++u)
{
if (dp[s][u] != INF) //选取其中实际可行的方案
{
for (int v = 0; v < n; ++v) //枚举下一步走的方案
{
if ((s & (1 << v)))//v如果已经走过了就没有必要再走
continue;
int news = s | (1 << v);
dp[news][v] = min(dp[news][v], dp[s][u] + E[u][v]);
}
}
}
}
printf("%d\n", dp[(1 << n) - 1][0]);
}
return 0;
}
HDU 5418 Victor and World(状压DP+Floyed预处理)的更多相关文章
- HDOJ 5418 Victor and World 状压DP
水状压DP Victor and World Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java ...
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- HDU 6149 Valley Numer II 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- HDU 4906 Our happy ending (状压DP)
HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- HDU 4568 Hunter 最短路+状压DP
题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...
- HDU 1074 Doing Homework【状压DP】
Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...
随机推荐
- Java环境变量搭建(Windows环境)
变量名:JAVA_HOME 变量值:C:\Program Files (x86)\Java\jdk1.8.0_91 // 要根据自己的实际路径配置 变量名:CLASSPATH 变量值:. ...
- 三种序列化方式存取redis的方法
常见的的序列化反序列方式的效率: protoBuf(PB) > fastjson > jackson > hessian > xstream > java 数据来自于:h ...
- js字符串的使用
Javascript的内置功能之一就是字符串连接,如果+号用于两个字符串连接 var s="hello,world" //想要查找给定位置的字符 s.cha ...
- checkbox 最多选两项
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 【转】C++ 标准库值操作迭代器的常见函数
迭代器是C++标准库中的重要组件,特别是在容器内部,没有迭代器,容器也就无所谓存在了. 例如:vector容器简而言之就是3个迭代器 start finish 以及end_of_storage vec ...
- 大蟒蛇肚子的"风暴"
遇到了数据库连接数不足的问题,一般情况下会预留一些会话增加的情况,但在一些特殊情况下如连接风暴(logon storm), 如果在监听中没有做rate限流,对数据库来说巨大的冲击可能会导致数据库Han ...
- 爬虫学习(九)——登录获取cookie爬取
import urllib.requestimport urllib.parseimport http.cookiejar # http.cookiejar 该包是专门对网页的cookie只进行获取的 ...
- ZendFramework-2.4 源代码 - 关于MVC - Model层
所谓的谓词Predicate // ------ 所谓的谓词 ------ // 条件 case.3 $where = new \Zend\Db\Sql\Where(); $expression = ...
- Hive如何根据表中某个字段动态分区
使用hive储存数据时,需要对做分区,如果从kafka接收数据,将每天的数据保存一个分区(按天分区),保存分区时需要根据某个字段做动态分区,而不是傻傻的将数据写到某一个临时目录最后倒入到某一个分区,这 ...
- 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
a,b,c,d,e,f,g=1,2,3,4,5,8,9 m = a > b and c < d or c > e n = b > a or g < f x = m and ...