问题描述 :

After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn’t want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.

输入:

There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.

输出:

There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.

样例输入:

2 1
1 2 100
3 2
1 2 40
2 3 50
3 3
1 2 3
1 3 4
2 3 10

样例输出:

100
90
7

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[60000][11];
int cnt[11][11];
int three[12];
int m,n;
int f[60000][11];
void init()
{
three[1]=1;
for(int i=2;i<=11;i++)
three[i]=three[i-1]*3;
for(int i=0;i<three[11];i++)
{
int tmp=i;
for(int j=1;j<=10;j++)
{
f[i][j]=tmp%3;
tmp/=3;
}
}
}
int main()
{
init();
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0x7f,sizeof(dp));
memset(cnt,0x7f,sizeof(cnt));
int ans=dp[0][0];
int inf=ans;
int x,y,d;
while(m--)
{
scanf("%d%d%d",&x,&y,&d);
cnt[x][y]=cnt[y][x]=min(cnt[x][y],d);
}
for(int i=1;i<=n;i++)
dp[three[i]][i]=0;
for(int state=1;state<three[n+1];state++)
{
bool ok=1;
for(int i=1;i<=n;i++)
{
if(f[state][i]==0)
ok=0;
if(dp[state][i]==inf)
continue;
for(int j=1;j<=n;j++)
{
if(i==j)
continue;
if(f[state][j]==2)
continue;
if(cnt[i][j]==inf)
continue;
dp[state+three[j]][j]=min(dp[state+three[j]][j],dp[state][i]+cnt[i][j]);
}
}
if(ok)
{
for(int i=1;i<=n;i++)
{
ans=min(ans,dp[state][i]);
}
}
}
if(ans==inf)
ans=-1;
printf("%d\n",ans);
}
return 0;
}

【算法】DP解决旅行路径问题的更多相关文章

  1. 蓝桥杯 试题 算法提高 宰羊 DP解决

    问题描述 炫炫回了内蒙,肯定要吃羊肉啦,所有他家要宰羊吃. 炫炫家有N只羊,羊圈排成一排,标号1~N.炫炫每天吃掉一只羊(这食量!其实是放生啦),吃掉的羊的邻居会以为它被放生了,然后又会告诉他们的邻居 ...

  2. POJ 3264 RMQ问题 用dp解决

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; ; #d ...

  3. Manacher (马拉车) 算法:解决最长回文子串的利器

    最长回文子串 回文串就是原串和反转字符串相同的字符串.比如 aba,acca.前一个是奇数长度的回文串,后一个是偶数长度的回文串. 最长回文子串就是一个字符串的所有子串中,是回文串且长度最长的子串. ...

  4. 北京地铁站点遍历最少经站次数问题普遍意义上是一个NP问题,目前不存在多项式时间算法能够解决该问题

    http://www.cnblogs.com/jiel/p/5852591.html 众所周知求一个图的哈密顿回路是一个NPC问题: In the mathematical field of grap ...

  5. [转贴]C语言复习笔记-17种小算法-解决实际问题

    判断日期为一年中的第几天(考虑闰年) /* * 计算该日在本年中是第几天,注意闰年问题 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 * 特殊情况,闰年且输入月份大于3时 ...

  6. Java或web中解决所有路径问题

    Java开发中使用的路径,分为两种:绝对路径和相对路径.归根结底,Java本质上只能使用绝对路径来寻找资源.所有的相对路径寻找资源的方法,都不过是一些便利方法.不过是API在底层帮助我们构建了绝对路径 ...

  7. UVA1292-----Strategic game-----树形DP解决树上的最小点覆盖问题

    本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...

  8. 【C/C++】C语言复习笔记-17种小算法-解决实际问题

    判断日期为一年中的第几天(考虑闰年) /* * 计算该日在本年中是第几天,注意闰年问题 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 * 特殊情况,闰年且输入月份大于3时 ...

  9. request.getContextPath是为了解决相对路径的问题,可返回站点的根路径

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

随机推荐

  1. 一些网站的meta标签的作用

    转载:https://www.cnblogs.com/Lily-nercel/p/6693293.html <!DOCTYPE html> <html lang="en&q ...

  2. Linux系统清除缓存

    1)缓存机制介绍在Linux系统中,为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果没 ...

  3. 装了appserv之后,浏览器中访问localhost加载不了

    AppServe下载地址:https://AppServnetwork.com/ 如果只下载Apache,推荐大神博客http://www.cnblogs.com/zhaoqingqing/p/496 ...

  4. TZOJ 2588 Bad Grass(DFS)

    描述 Bessie was munching on tender shoots of grass and, as cows do, contemplating the state of the uni ...

  5. HDU 6214 Smallest Minimum Cut(最少边最小割)

    Problem Description Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition o ...

  6. [剑指Offer]40-最小的k个数

    题目链接 https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf?tpId=13&tqId=11182&t ...

  7. Spring配置文件XML详解

    1.bean的基本属性配置: <!-- id是bean的标识符,必须唯一,如果没有配置id,name默认为标识符 如果配置了id,有配置了name,那么name为别名 name可以设置多个别名, ...

  8. 工作中用Git对项目进行管理

    前言 之前一直是用svn来管理代码的,今天第一次用git来管理代码,从安装.上传代码过程中遇到了很多问题,Github中建的repository之前还是https协议,最后不知道怎么又变成了git协议 ...

  9. YII2开启路由配置后,新加的模块无法访问

    最近使用YII2,自定义创建了一个自定义模块users,位置为app\modules\users. 'modules' => [ 'users' => [ 'class' => 'a ...

  10. gearman中worker常驻后台,导致MySQL server has gone away

    产生这个原因主要有如下几点: 1.mysql服务宕机了 2.长时间没有操作,超过了wait_timeout的设置,mysql自动断开 3.mysql请求链接被主动kill 4.发送的请求或返回结果过大 ...