Travelling
Travelling
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2554 Accepted Submission(s): 746
Problem Description
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.
Input
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.
Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
Sample Input
2 1
1 2 100
3 2
1 2 40
2 3 50
3 3
1 2 3
1 3 4
2 3 10
Sample Output
100
90
7
Source
2009 Multi-University Training Contest 11 - Host by HRBEU
Recommend
gaojie
把所有的城市访问次数合在一起作为状态的描述,因为每个城市最多访问2次,所以状态压缩的结果是3进制的.
状态转移方程:DP[i][j]=Min(DP[i.pre][k]+G[k][j])
DP[i][j]表示达到i状态并且此时位于j城市所需要的最小花费.
#include<stdio.h>
#include<string.h>
int N,M;
int state[][];
int DP[][];
int len[];
int G[][];
int s[];
int f(int x)
{
int i;
for (i=;i<;i++)
if (state[x][i]==) return i;
return ;
}
void init()
{
int i,j;
s[]=;
for (i=;i<;i++) s[i]=s[i-]*;
for (i=;i<s[];i++)
{
int tmp=i;
for (j=;j<;j++)
{
state[i][j]=tmp%;
tmp/=;
}
}
for (i=;i<s[];i++) len[i]=f(i);
}
int main()
{
init();
while (scanf("%d%d",&N,&M)!=EOF)
{
int i,j,k;
memset(G,0x3f,sizeof(G));
for (i=;i<=M;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
u--;
v--;
if (c<G[u][v]) G[u][v]=c;
if (c<G[v][u]) G[v][u]=c;
}
memset(DP,0x3f,sizeof(DP));
for (i=;i<N;i++) DP[s[i]][i]=;
for (i=;i<s[N];i++)
for (j=;j<N;j++)
if (state[i][j])
{
int last=i-s[j];
for (k=;k<N;k++)
if (DP[last][k]+G[k][j]<DP[i][j] && state[last][k])
DP[i][j]=DP[last][k]+G[k][j];
}
int Min=;
for (i=;i<s[N];i++)
if (len[i]==N)
for (j=;j<N;j++)
if (state[i][j] && DP[i][j]<Min)
Min=DP[i][j];
if (Min==) printf("-1\n");
else printf("%d\n",Min);
}
return ;
}
Travelling的更多相关文章
- ACM: 限时训练题解- Travelling Salesman-最小生成树
Travelling Salesman After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...
- Codeforce - Travelling Salesman
After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends most of his time travelli ...
- URAL 1077 Travelling Tours(统计无向图中环的数目)
Travelling Tours Time limit: 1.0 secondMemory limit: 64 MB There are N cities numbered from 1 to N ( ...
- HDU-3001 Travelling
http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ...
- Bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 动态规划
1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1006 Solved: ...
- BZOJ1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 762 Solved: ...
- BZOJ 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛( dp )
一道水 dp ...然后我一开始用 BFS ...结果 MLE 了... dp[ i ][ j ][ k ] 由它四个方向上的 k - 1 转移. -------------------------- ...
- hdu 3001 Travelling (TSP问题 )
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 3001 Travelling(状态压缩 三进制)
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- iphone数据存储之-- Core Data的使用(一)
http://www.cnblogs.com/xiaodao/archive/2012/10/08/2715477.html 一.概念 1.Core Data 是数据持久化存储的最佳方式 2.数据最终 ...
- Coursera台大机器学习技法课程笔记04-Soft-Margin Support Vector Machine
之前的SVM非常的hard,要求每个点都要被正确的划分,这就有可能overfit,为此引入了Soft SVM,即允许存在被错分的点,将犯的错放在目 标函数中进行优化,非常类似于正则化. 将Soft S ...
- 对Excel文件的操作
①.将文件设为“嵌入的资源”,Template修改不灵活:Stream stream=this.GetType().Assembly.GetManifestResourceStream(Templat ...
- Python发布Django项目的pyc版脚本
import os import sys from py_compile import compile #print "argvs:",sys.argv if len(sys.ar ...
- wget 增加单个文件下载限制大小
增加了参数 -M --limit-size 使用方法 -M 5m 或者 -M 500k 或者 --limit-size=5m 或者 --limit-size=500k 下载地址 http://pan. ...
- Java for LeetCode 064 Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- codeforces A. Sereja and Bottles 解题报告
题目链接:http://codeforces.com/problemset/problem/315/A 题目意思:有n个soda bottles,随后给出这n个soda bottles的信息.已知第 ...
- Docker跨主机通信之路由
一.实验环境: 主机名 主机IP Docker0_IP Docker1 192.168.88.130 172.17.0.1 Docker2 192.168.88.131 172.18.0.1 二.实验 ...
- C++基础(1)
1.关于继承及访问. C++中 public,protected, private 访问标号小结,即访问标号使用限制. 第一:private, public, protected 访问标号的访问范围. ...
- git linux
第一节 GIT最初是由Linus Benedict Torvalds为了更有效地管理Linux内核开发而创立的分布式版本控制软件,与常用的版本控制工具如CVS.Subversion不同,它不必服务器端 ...