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)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- [NOIp2017提高组]宝藏
#include<cstdio> #include<cctype> #include<algorithm> inline int getint() { regist ...
- jsp ajax实例讲解
下面介绍JSP前台表单内容通过Ajax异步提交到后台Servlet进行校验(校验方式多种,包括提取数据库信息,校验用户名是否重复等),异步在JSP表单页面显示校验结果信息的基本过程. 一.说明: 1. ...
- EF for Oracle 闪退
EF oracle 加入实体类型时候闪退 主要原因: Oracle.ManagedDataAcces 版本和 SetupODTforVS2015 版本不一致所致. 更新后 SetupODTforVS2 ...
- Word中将图表变为表格
一定要先拷贝了图表,否则第二张图片没那个像是,只有最后一个,这样的做的目的是减少查重. 还有就是把部分文字放入Mathtype,查不出来.
- 使用Git代替FTP进行虚拟主机的代码管理
为什么要使用Git代替FTP的原因: 由于本人菜鸟+穷屌,玩不起VPS和其他大牌的云主机,所以呢就只能在景安(这不是广告..)申请了免费的虚拟主机,就想着自己玩玩而已,免费的嘛,空间流量什么的就不讨论 ...
- Android编程实用代码合集
1.android dp和px之间转换public class DensityUtil { /** * 根据手机的分辨率从 dip 的单位 转成为 px(像素) */ public static in ...
- php判断手机客户端
<?php // check if wap function check_wap(){ if(stristr($_SERVER['HTTP_VIA'],"wap")){// ...
- Java构造和解析Json数据的两种方法详解一——json-lib
转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html 在www.json.org上公布了很多JAVA下的jso ...
- laravel的 array 函数
代码如下: routes.php文件 // 获⃣取⃣数⃣组⃣的⃣第⃣一⃣个⃣ Route::get('/helper', function () { $arr = [1, 2, 4]; return ...
- gmock学习01---Linux配置gmock
本文目的 本文主要介绍gmock 1.6.0版本在Linux上如何部署和使用. gmock是做什么的? 使用C++手动编写mock对象将会是一件十分耗时,易于出错,枯燥乏味的事情.gmock提供一整套 ...