HDOJ 5418 Victor and World 状压DP
水状压DP
Victor and World
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 407 Accepted Submission(s): 164
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.
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.
: the i-th
of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel.
1
3 2
1 2 2
1 3 3
10
/* ***********************************************
Author :CKboss
Created Time :2015年08月23日 星期日 10时27分21秒
File Name :HDOJ5418.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert> using namespace std; typedef long long int LL; const int INF=0x3f3f3f3f; int n,m;
int G[18][18];
int dp[18][1<<18]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
memset(G,63,sizeof(G));
for(int i=0;i<n;i++) G[i][i]=0;
for(int i=0,a,b,c;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
a--; b--;
G[a][b]=min(G[a][b],c);
G[b][a]=min(G[b][a],c);
} for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
memset(dp,63,sizeof(dp)); dp[0][1]=0;
for(int i=1;i<(1<<n);i++) /// status
{
for(int j=0;j<n;j++) /// from point
{
if(((1<<j)&i)!=0)
{
for(int k=0;k<n;k++) /// to point
{
if(((1<<k)&i)==0)
{
dp[k][((1<<k)|i)]=min(dp[k][((1<<k)|i)],dp[j][i]+G[j][k]);
}
}
}
}
}
int ans=INF;
for(int i=0;i<n;i++)
{
ans=min(ans,dp[i][(1<<n)-1]+G[i][0]);
}
if(n==1) ans=0;
printf("%d\n",ans);
}
return 0;
}
HDOJ 5418 Victor and World 状压DP的更多相关文章
- [hdu5418 Victor and World]floyd + 状压DP 或 SPFA
题意:给n个点,m条边,每次只能沿边走,花费为边权值,求从1出发经过所有其它点≥1次最后回到1的最小花费. 思路: 状压DP.先用Floyd得到任意两点间的最短距离,转移时沿两个点的最短路转移.此时的 ...
- POJ 3311 Hie with the Pie (状压DP)
题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...
- 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 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- Java源代码编译过程
编译其本质是将一种语言规范转换成另一种语言规范,即将Java语言规范转换为JVM虚拟机语言规范.结果就是.java文件到.class文件. 对于C/C++编译直接将高级语言转换为机器语言,Java ...
- Everything | 本地文件搜索引擎工具
文章目录 本地文件搜索软件 下载地址 注意 本地文件搜索软件 您是否觉得 Windows 自带的文件搜索比较慢?! 今天,给大家分享一款超快好用的本地文件搜索软件:Everything Search ...
- ob_flush()和flush()和ob_implicit_flush(true)
http://www.jb51.net/article/16215.htm 第一: “ob_flush()和flush()的区别.前者是把数据从PHP的缓冲中释放出来,后者是把不在缓冲中的或者说是被释 ...
- (转)CString,int,string,char*之间的转换
CString,int,string,char*之间的转换http://www.cnblogs.com/greatverve/archive/2010/11/10/cstring-int-string ...
- asp.net 分布式应用开发
Net Framework推出的许多新技术为上述任务的实现提供了相对简单的解决方案.其中,基于SOAP的Web Service在处理分布式应用时具有比传统的DCOM/CORBA明显的优点,结合基于We ...
- 如何优雅的编写Objective-C语言?
① 减少缩写 命名缩写只用于通用专业术语,如URL,不可自创命名缩写,如Ctr.Msg.命名宁可长一些,也不要难于理解. ② 过程化 动作发生之前用Will,发生之后用Did,询问是否发生用Shoul ...
- WebSocket 是什么原理?为什么可以实现持久连接?(转载)
本文转载自知乎,来源如下: 作者:Ovear链接:https://www.zhihu.com/question/20215561/answer/40316953来源:知乎著作权归作者所有.商业转载请联 ...
- Android获取wifi MAC,关闭的wifi不能获取
最近一直在尝试着在Android上通过ndk用C++获取本机MAC地址,但是用ioctl(SIOCGIFHWADDR)时,一直返回值小于0,即获取不到接口信息.刚开始我以为是自己漏掉了哪些访问权限,所 ...
- 较详细的介绍JNI
JNI其实是Java Native Interface的简称,也就是java本地接口.它提供了若干的API实现了和Java和其他语言的通信(主要是C&C++).也许不少人觉得Java已经足够强 ...
- scikit-learn(window,linux)安装
scikit-learn是python的机器学习库 记录下载window中和linux中如何下载scikit-learn 方法一 直接下载Anaconda 这是一个非常齐全的python发行版本,里面 ...