问题描述 :

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. Pandas之数据结构

    pandas入门 由于最近公司要求做数据分析,pandas每天必用,只能先跳过numpy的学习,先学习大Pandas库 Pandas是基于Numpy构建的,让以Numpy为中心的应用变得更加简单 pa ...

  2. shell中参数的传递

    1.命令行参数 向shell脚本传递数据的最基本方式是使用命令行参数. (1) 读取参数 读取输入的参数的变量为位置参数,位置参数通过标准数字表示, 其中$0为程序名称,$1为第一个参数,$2为第二个 ...

  3. 如何利用FPGA进行时序分析设计

    FPGA(Field-Programmable Gate Array),即现场可编程门阵列,它是作为专用集成电路(ASIC)领域中的一种半定制电路而出现的,既解决了定制电路的不足,又克服了原有可编程器 ...

  4. SQLite4Unity3d

    What's this? When I started with Unity3d development I needed to use SQLite in my project and it was ...

  5. Unity4.6证书激活问题

    第一次运行的激活问题 安装好Unity4.6之后首次启动会自动连网,但是会出现error loading page错误,SSL peer certificate or SSH remote key w ...

  6. GridView和DataFormatString 日期格式 精确小数点后位数

    如果DataFormatString无效,请添加属性 HtmlEncode = "false" --------------------------------------- Da ...

  7. day 26 初识进程,验证客户端合法性

    验证客户端合法性: # 1.需要认证 # 程序和用户打交道的时候才会用到用户认证 # 对所有的客户端进行统一的认证 # 我现在要做的事情 # 写一个server端 # 写一个client端 特殊的 符 ...

  8. AngularJS——第9章 模块加载

    第9章 模块加载 AngularJS模块可以在被加载和执行之前对其自身进行配置.我们可以在应用的加载阶段配置不同的逻辑. [AngularJS执行流程] 启动阶段(startup) 开始 --> ...

  9. This system is not registered with an entitlement server. You can use subscription-manager to register.

    错误信息 [root@bogon apache-tomcat-]# yum install gcc-c++ Loaded plugins: product-id, search-disabled-re ...

  10. 如何在Oracle中 查询一个表被其他数据库对象引用[z]

    这两天老大让我再oracle中把要替换的表被其他对象引用之处找出来,整理一份表,接到这个任务,我是一脸懵逼,怎么找?大海捞针么?问同事.查资料,自己研究,最后整理一下仅供大家参考,同时以备将来回顾.本 ...