问题描述 :

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. redis做消息列队

    #encoding:utf8 import time import redis conn = redis.Redis('localhost',db=1) #连接诶数据库并使用数据库1 def inse ...

  2. day31 粘包问题

    TCP粘包问题 cmd客户端代码 import socket import struct import socket import json c = socket.socket() c.connect ...

  3. Java输入输出流详解2

    InputStream/Reader:所有输入流的基类,只能从中读取数据: OutputStream/Writer:所有输出流的基类,只能向其写入数据.

  4. Docker 网络不通的解决方法

    表现是: docker主机内部网络正常,与其它主机的连接失效,其它主机不能连接docker主机上映射的端口,docker内部也无法连接外部主机. 执行docker info,可以看到一些警告. 可在不 ...

  5. VM 端口映射问题

    环境: 宿主机:WIN 10 --192.168.9.87 虚拟机:CentOS ---192.168.255.129 联通模式:NAT模式 一.VM做端口映射 1.为了方便管理,设置虚拟机为静态IP ...

  6. js前端导出excel:json形式的导出

    第一中形式的导出:主要是表头对应主体数据,json形式的导出 js库文件名称 : table2excel.js这个js库文件是网上找的,并且自己根据自己业务需求把内容改了一下复制到 table2exc ...

  7. 100-days:nine

    Title: Boeing(波音飞机) crash isolates FAA as(伴随,随着) China leads push against Max(出事机型,即737 Max) crash n ...

  8. Windows 64 位 mysql 5.7.20 安装教程

    mysql 5.7以上版本包解压中没有data目录和my-default.ini和my.ini文件以及服务无法启动的解决办法以及修改初始密码的方法 mysql官网下载地址:https://dev.my ...

  9. Server2003+IIS6+TP-Link+花生壳配置

    Server2003+IIS6+TP-Link+花生壳配置外网一共分四步: 固定Server2003电脑的局域网IP地址. 设置IIS网站中的TCP端口. 在TP-Link中设置转发规则. 申请花生壳 ...

  10. Java_7 ArrayList集合

    1 ArrayList创建变量的步骤 1: 导入包 java.util包中 2: 创建引用类型的变量 数据类型< 集合存储的数据类型> 变量名 = new 数据类型<集合存储的数据类 ...