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)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- 【转】Geary's C
李旭,Matlab: Geary's C 原文地址 Introduction Geary's C is a measure of spatial autocorrelation or an attem ...
- html模板与json数据交互
阅读这篇文章后,对html和json有很大的启发: http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/ ...
- 【LaTeX】E喵的LaTeX新手入门教程(2)基础排版
换了块硬盘折腾了好久..联想的驱动真坑爹.前情回顾[LaTeX]E喵的LaTeX新手入门教程(1)准备篇文档框架嗯昨天我们已经编写了一个最基本的文档,其内容是这样的:\documentclass{ar ...
- 怎么设置IDEA,去除单词拼写检查,或者添加自定义的单词
如图所示,添加自定义的单词,这样IDEA检查的时候,就不会报错了.估计默认是根据英文单词来释义的.
- iOS:iOS中的几种动画
本文来自收藏,感谢原创博主. iOS中的动画 摘要 本文主要介绍核iOS中的动画:核心动画Core Animation, UIView动画, Block动画, UIImageView的帧动画. 核心动 ...
- Windows系统下将目录挂载为一个磁盘并分配盘符
Windows系统下subst可以临时将目录分配一个盘符. 将路径与驱动器号关联. SUBST [drive1: [drive2:]path]SUBST drive1: /D drive1: 指定要分 ...
- HDU oj password
#include<stdio.h> #include<string.h> main() { int m; scanf("%d",&m); ch ...
- 可以ping通虚拟机但不能telnet 9000端口
突然发现eclipse不能连上虚拟机了,报错是本机连接不上9000的端口. 觉得有点奇怪,就在命令行里试图ping通虚拟机,成功:但尝试这telnet 9000端口的时候,却报错连接不上. 上网查了这 ...
- web页面实时更新页面的原理--WebSocket
原文:https://www.jianshu.com/p/8f956cd4d42b angular-cli启动的项目也可以自动刷新,底下应该也是应用的websocket的原理. ----------- ...
- shell的浮点运算
参考: https://www.linuxquestions.org/questions/linux-software-2/multiply-floats-in-bash-script-618691/ ...