水状压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. Spring p名称空间配置属性

    1.p 名称空间介绍 从 2.0开始,Spring支持使用名称空间的可扩展配置格式.这些名称空间都是基于一种XML Schema定义.事实上,我们所看到的所有bean的配置格式都是基于一个 XML S ...

  2. (转) Unity3D常用代码收集总结

    //创建一个名为"Player"的游戏物体 //并给他添加刚体和立方体碰撞器. player=new GameObject("Player"); player. ...

  3. Nginx 重定向 伪静态 rewrite index.php

    参考https://www.kancloud.cn/manual/thinkphp5/177576 thinkphp入口文件同目录下添加.把下面的内容保存为.htaccess文件 <IfModu ...

  4. jQuery.extend重载合并

    使用: var people1 = { apple: 0, banana: { weight: 52, price: 100 }, age: 97 }; var people2 = { banana: ...

  5. Android开发高级进阶——多进程间通信

    一. 什么是多进程? 多进程就是多个进程的意思,那么什么是进程呢? 当一个应用在开始运行时,系统会为它创建一个进程,一个应用默认只有一个进程,这个进程(主进程)的名称就是应用的包名. 进程的特点: 进 ...

  6. webService 三要素

    WebService(jax-ws)三要素 SOAP: 基于HTTP协议,采用XML格式,用来传递信息的格式. WSDL: 用来描述如何访问具体的服务.(相当于说明书) UDDI: 用户自己可以按UD ...

  7. jQuery 当页面 ID 不唯一时,寻找元素

     $(需要寻找元素的父层).find(元素ID,元素Class) 

  8. jQuery匹配各种条件的选择器用法

    :hidden匹配所有的不可见元素,input 元素的 type 属性为 "hidden" 的话也会被匹配到Matches all elements that are hidden ...

  9. WAF攻击与防御

    背景 对于腾讯的业务来说,有两个方面决定着WAF能否发挥效果,一个是合适处理海量流量的架构,另一个关键因素则是规则系统.架构决定着WAF能否承受住海量流量的挑战,这个在之前的篇章中简单介绍过(详情见主 ...

  10. go语言基础之常量

    1.常量 示例: package main //必须有一个main包 import "fmt" func main() { //变量:程序运行期间,可以改变的量, 变量声明需要va ...