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

Problem Description
After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to n. They are connected by m undirected flights, detailedly the i-th flight connects the ui-th and the vi-th country, and it will cost Victor's airplane wi L fuel if Victor flies through it. And it is possible for him to fly to every country from the first country.

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.

 
Input
The first line of the input contains an integer T, denoting the number of test cases.
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.

 
Output
Your program should print T lines : the i-th of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel.
 
Sample Input
1
3 2
1 2 2
1 3 3
 
Sample Output
10
 

题目链接: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预处理)的更多相关文章

  1. HDOJ 5418 Victor and World 状压DP

    水状压DP Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java ...

  2. ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

    HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & ...

  3. HDU 6149 Valley Numer II 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...

  4. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  5. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

  6. HDU 4906 Our happy ending (状压DP)

    HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...

  7. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  8. HDU 4568 Hunter 最短路+状压DP

    题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...

  9. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

随机推荐

  1. 运维如何延续自己的职业生涯--萧田国2017年GOPS深圳站演讲内容

    正如 萧田国在2017年GOPS深圳站演讲所提及的,运维职业生涯规划,应该是T字型. 关于指导原则,正如腾讯好友@赵建春所言: 如果一个领域不能做到TOP,那就是一种伤害. 运维在编程.开发领域,能做 ...

  2. C# 多态与new关键字

    1. 你通常怎样用多态? 假设我有一个类,里面有一个 PrintStatus 方法,用于打印实例的当前状态,我希望该类的派生类都带有一个 PrintStatus 方法,并且这些方法都用于打印其实例的当 ...

  3. 基于supersocket、C#对JT808协议进行解析构建gps监控平台服务端

    GPS监控平台.车联网.物联网系统中GPRS网络数据的并发通讯和处理解析,主要功能有socket的UDP和TCP链路建立和维持,网络数据协议包接收与解析,分发上传到其他业务规则服务器,在物联网以及位置 ...

  4. Oracle数据库学习(三)

    6.关于null 数据库中null是一个未知数,没有任何值:进行运算时使用nvl,但是结果仍为空:在聚集函数中只有全部记录为空才会返回null. 7.insert插入 (1)单行记录插入 insert ...

  5. 你所不知道的js的小知识点(1)

    1.js调试工具 debugger <div class="container"> <h3>debugger语句会产生一个断点,用于调试程序,并没有实际功能 ...

  6. Too Rich HDU - 5527 (贪心+dfs)

    Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  7. windows_Bat_Scripts查看系统IP-更改regedit-更新系统补丁

    1.1    脚本名称 Update_patch.bat 1.2    脚本代码 @echo off :menu cls mode con cols=48 lines=27 & color 0 ...

  8. Python编写一个程序求2的次方

    #!/usr/bin/env python3 #-*- coding:utf-8 -*- #":"冒号后面为对参数注释,"->"为对整个函数注释 def ...

  9. 爬虫之Scarpy.Request

    一 .Request 1.request Scarpy中的HTTP请求对象 1.1.Requse的构造 #我们ctrl+左键可以看到Scarpy.Request的代码 class Request(ob ...

  10. 2017 United Kingdom and Ireland Programming(Gym - 101606)

    题目很水.睡过了迟到了一个小时,到达战场一看,俩队友AC五个了.. 就只贴我补的几个吧. B - Breaking Biscuits Gym - 101606B 旋转卡壳模板题.然后敲错了. 代码是另 ...