水状压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

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
 
Source
 

/* ***********************************************
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的更多相关文章

  1. [hdu5418 Victor and World]floyd + 状压DP 或 SPFA

    题意:给n个点,m条边,每次只能沿边走,花费为边权值,求从1出发经过所有其它点≥1次最后回到1的最小花费. 思路: 状压DP.先用Floyd得到任意两点间的最短距离,转移时沿两个点的最短路转移.此时的 ...

  2. POJ 3311 Hie with the Pie (状压DP)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  3. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  4. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  5. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  6. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  7. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  8. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  9. HDU 1074 Doing Homework (状压dp)

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

随机推荐

  1. Java编程思想学习(三)----第三章:操作符

    3.2使用Java操作符 操作符接受一个或多个参数,并生成一个新值. 操作符作用于操作数,生成一个新值.有些操作符可以改变操作数自身的值.几乎所以的操作符都只能操作“基本类型”.例外的操作符有“=”, ...

  2. Maven打war包时,添加本地jar包

    1.在项目根目录中新建lib文件夹,添加jar包 2.在pom.xml文件中添加dependency <dependency> <groupId>com.oracle</ ...

  3. Node.js+MySQL管理工作的详细信息所遇到的问题

    问题陈述: Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xE8\xB4\xAD\xE7\x89\xA9' ...

  4. HDU 5636 Shortest Path 暴力

    Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...

  5. FFT算法的完整DSP实现

    傅里叶变换或者FFT的理论参考: [1] http://www.dspguide.com/ch12/2.htm The Scientist and Engineer's Guide to Digita ...

  6. Pointers and Strings

    The special relationship between arrays and pointers extends to C-style strings.Consider the followi ...

  7. 黑莓10开发101:Native平台

    为黑莓10开发游戏或应用程序有多种方法.这是件好事,不过如果你不是在黑莓平台下进行开发,或者你仅熟悉基于旧的Java的开发平台,也许你会感到无所适从.你将作何选择?从哪里着手? 促成你做出决定的因素有 ...

  8. 用xib自定义UIView并在代码中使用--iOS

    首先新建一个空的xib文件: 将size改为freedom: 然后在xib中自定义视图(添加自己想要的其它视图): 要写好约束, 创建一个继承uiview的类和他关联,然后就可以调用了. - (voi ...

  9. saltstack常用语法

    一.常用语法 1.添加用户 示例1: #添加zabbix用户和组 zabbix: group.present: - name: zabbix - gid: 1001 user.present: - f ...

  10. springmvc怎么重定向,从一个controller跳到另一个controller

    第一种情况,不带参数跳转: 方法一:使用ModelAndView return new ModelAndView("redirect:/toList");  这样可以重定向到toL ...