The Best Travel Design
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1359   Accepted: 340

Description

Dou Nai is an excellent ACM programmer, and he felt so tired recently that he wants to release himself from the hard work. He plans a travel to Xin Jiang .With the influence of literature, he wishes to visit Tian Chi, Da Ban Town, Lou Lan mysterious town , Yi Li , and other sights that also have great attraction to him. But the summer vocation time is not long. He must come back before the end of the summer vocation. For visiting more sights and all the necessary sights, he should make a thorough plan. Unfortunately, he is too tired to move, so you must help him to make this plan. Here are some prerequisites: there are two ways of transportation, bus and train, and velocity of the bus is 120km/h and the train is 80km/h. Suppose the travel is started from Urumuqi (point 1), and the end of the travel route is Urumuqi too. You need to spend some time to visit the sights, but the time of each visit is not always equal. Suppose we spend 12 hours on traveling every day.

Input

There are several test cases. For each case, the first line is three integers N, M and K. N (1<=n<=15) is the number of sights, M(0<=M<=N) is total sights he must arrived (sight 1 is always must be arrived) and K is total traveling time (per day). The second line is M integers which sights he must visited. The third line is N integers, the i th integer means the time he will stay in the sight i (per hour). Then several lines follow. Each line is four integers x, y, len and kind, 1<=x, y<=n, 0<len<=1000, means there is a bidirectional path between sights x and y, the distance is len, kind=0 means x and y are connected by train, kind=1 is by bus.
x=y=len=kind=0 means end of the path explanation.

N=M=K=0 means end of the input.

Output

For each case, output maximum sights he will travel with all necessary sights visited or "No Solution" if he can't travel all the sights he like best in time.

Sample Input

3 3 3
1 2 3
10 8 6
1 2 120 0
1 3 60 1
2 3 50 1
0 0 0 0
3 3 2
1 2 3
10 8 6
1 2 120 0
1 3 60 1
2 3 50 1
0 0 0 0
0 0 0

Sample Output

3
No Solution

Source

题意:
很多旅游景点,在给定时间内,给定要游览的点。点与点之间由不同长度的路连接。有两种交通方式------火车和BUS,速度不同且已知。从起点1出发,要求在给定时间内
把给定景点游览完且回到起点1,并且游览的景点最多。输出最多的游览个数。如果给定景点在限定时间内不能都游览完则输出no solution.(大写)。每个景点的stay的时间
也已知。

注:给定游览时间以天计算,而且每天的游览时间为12个小时。


代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#define INF 1e9
using namespace std; double a[20],t;
double map[22][22];
double dp[20][1<<20];
int n,m; void init()
{
int i,j;
for(i=0;i<n;i++)
{
map[i][i]=0;
for(j=0;j<n;j++)
{
map[i][j]=INF;
}
for(j=0;j<(1<<n);j++) //所有状态下i为终点的用时都初始化为无穷大
{
dp[i][j]=INF;
}
}
}
void floyd()
{
int i,j,k;
for(k=0; k<n; k++) //k为i和j之间的点
{
for(i=0; i<n; i++)
{
if(i!=k&&map[i][k]<INF)
for(j=0; j<n; j++)
{
if(i!=j&&map[k][j]<INF)
{
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
}
}
}
}
} int main()
{
int res,cnt,tmp,ans,x,y,kind,len,i,j,k;
while(scanf("%d%d%lf",&n,&m,&t),(n||m||t))
{
res=0,ans=-1;
double day=t*12.0;
init();
for(i=1;i<=m;i++)
{
scanf("%d",&k);
res+=1<<(k-1); //res记录要访问的所有点的状态,便于之后对照。
}
for(i=0;i<n;i++)
scanf("%lf",&a[i]); //a[i]记录每个景点stay的用时
while(scanf("%d%d%d%d",&x,&y,&len,&kind),(x||y||len||kind))
{
x--,y--; //转化为以0为起点
double hour=len*1.0/(kind?120.0:80.0);
map[x][y]=min(hour,map[x][y]);
map[y][x]=min(hour,map[y][x]);
}
floyd();
for(i=1;i<n;i++)
dp[i][1<<i]=map[0][i]+a[i]; //初始化从起点直接到i的用时
for(j=0;j<(1<<n);j++) //枚举所有状态
{
for(i=0;i<n;i++) //在起点0和i之间取点k来更新最短用时,dp实现
{
if((j&(1<<i))&&j!=(1<<i)) //j状态包含0-->i的状态且不等于那个状态
{
for(k=0;k<n;k++)
{
if((j&(1<<k)&&i!=k&&j!=(1<<k)))
dp[i][j]=min(dp[i][j],dp[k][j-(1<<i)]+map[k][i]+a[i]);
}
if(((j&res)==res)&&map[i][0]+dp[i][j]<=day) //如果j状态包含res记录的状态且用时小于等于限定的时间
{
tmp=j;
cnt=0;
while(tmp) //j状态每一位的状态进行遍历
{
if(tmp%2) cnt++; //为1的位则cnt++
tmp=tmp>>1;
}
ans=max(cnt,ans); //更新最大值
//printf("test: %d\n",ans);
}
}
}
}
if(ans>=0)
printf("%d\n",ans);
else printf("No Solution\n");
}
return 0;
} //391MS


分析:
已经游览的每个点用1表示,未游览的用0表示。某时刻的游览状态就可以用一个整数的二进制形式表示。
dp[i][j]表示j状态下以i为终点的用时。
先用floyd处理每两点的最短时间。然后用floyd思想来进行动态规划。
dp方程: dp[i][j]=min(dp[i][j],dp[k][j-(1<<i)]+map[k][i]+a[i])
状态压缩的时候为了表示方便,把起点为1转化为起点为0。

如果以1为起点的话,确实不好处理。因为n个景点就有n种状态。每一位代表一个景点的状态。那么从1开始就只能用n位
表示n-1种状态。

转化为以编号0的点为起点只需每次把点的坐标前移一个即减1就行了。

注意:因为算速度的时候可能不能整除,故用double存与记录时间有关的变量为宜。并且相关的计算也要注意将整形转化
            为浮点型。


11989507

fukan

3229

Accepted

4148K

391MS

C++

2504B

2013-08-15 22:30:24

poj 3229 The Best Travel Design ( 图论+状态压缩 )的更多相关文章

  1. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

  2. POJ 1185 炮兵阵地(经典的状态压缩DP)

    题意:中文题. 思路,经典的状态压缩题目. 由于列长比较小,我们可以以行为阶段用状态压缩来做. 由于攻击只占两个格,这样从行的角度看,第i行的炮兵只与前i-1和前i-2行有关系.这样如果用j,k,l分 ...

  3. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

  4. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  5. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  6. POJ 2046 Gap 搜索- 状态压缩

    题目地址: http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash. AC代码: #include <iostream> #include ...

  7. poj2443(简单的状态压缩)

    POJ2443 Set Operation Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2679   Accepted:  ...

  8. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

  9. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

随机推荐

  1. 在Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)

    可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件来实现表单或者URL提交数据的获取,获取后传给SQLInje ...

  2. 自定义android精美聊天界面

    编写精美聊天界面,那就肯定要有收到的消息和发送的消息. 首先还是编写主界面,修改activity_chat.xml中的代码,如下所示: <?xml version="1.0" ...

  3. MySQL无法登录服务器解决方法

    提示:#2000 无法登录 MySQL 服务器 今天用本机装了个phpMyAdmin,版本3.4.8,想用它来连一台内网服务器上的Mysql,于是乎修改phpMyAdmin配置文件config.inc ...

  4. PHP MySQL 读取数据

    PHP MySQL 读取数据 从 MySQL 数据库读取数据 SELECT 语句用于从数据表中读取数据: SELECT column_name(s) FROM table_name 如需学习更多关于 ...

  5. ASP.NET菜鸟之路之Request小例子

    背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Request获取值 Request获取值有两种 ...

  6. Delphi 动态改变Rzsplitter的Orientation(方向)属性

    效果图: 原先不知道,弄了半天都改不了RzSplitter.Orientation = orHorizontal / orVertical 然后去查该组件的源代码,原来Orientation不是在Rz ...

  7. 【NEERC 2003】有向图破坏

    [题目描述] Alice和Bob正在玩如下的游戏.首先Alice画一个有N个顶点,M条边的有向图.然后Bob试着摧毁它.在一次操作中他可以找到图中的一个点,并且删除它所有的入边或所有的出边. Alic ...

  8. 解析一下rtmp协议比较难懂的地方

    官方文档写的过于复杂,这里弄个简单的好入门的.chunk 分基础头,消息头,时间戳,数据部分基础头中第一个字节最高位的两个位是用来设置消息头的四种格式的,和基础头没关系,整个基础头有3个字段的长度存储 ...

  9. shell每日发邮件

    LOGFILE="$fank/"`date +"%Y%m%d"`"data"#每日文件 from="abc@123.com&quo ...

  10. C盘不能新建文件的问题解决办法

    C盘不能新建文件的问题解决办法 主要症状: 1.C 盘文件不能修改2.C 盘不能新建文件3.总之就是只能读取不能,写入和修改这样对于平时操作造成了极其的不方便~~~复制文件到C 盘会提示:错误0×80 ...