算法思想题

qiyudong 2009-08-16 01:24:17
今天在做acm,遇到这个题目真是老虎吃天,无从下嘴。大侠们都进来帮帮忙,有思想提供思想,有算法提供算法,有代码,贴上核心代码。多谢了!


Problem description
Superior Island is a very picturesque island and only bicycles are allowed on the island. Therefore, there are many one-way bicycle roads connecting the different best photo-shooting spots on the island. To help the visitors plan their trip to the island, the tourism commission wants to designate r different bicycle routes that go through some of the best photo-shooting spots on the island. Given a map of all the bicycle roads on the island and a list of the best photo-shooting spots to be included on each of the three planned routes (non-listed spots must not be included in the route), please write a program to plan each of the r routes so that the distance on each route is minimal. Note that each best photo-shooting spot may only appear at most once on the route.



Input
There are two parts to the input. The first part of input gives the information of the bicycle roads on the island. The first line contains two integer n and r, n ≤ 100 and r ≤ 10, indicating that there are n best photo-shooting spots on the island and there are r routes to be planned. The next n lines (line 2 through line n + 1) contains n × n integers (n lines with n integers on each line), where the jth integer on line i denotes the distance from best photo-shooting spot i − 1 to best photo-shooting spot j; the distances are all between 0 and 10, where 0 indicates that there is no one-way road going from best photo-shooting spot i − 1 to spot j.

The second part of input has r lines, denoting the r sightseeing routes to be planed. Each line lists the best photo-shooting stops to be included in that route. The integers on each line denote the recommended photo-shooting stops on that particular sightseeing route. The first integer on the line is the starting point of the route and the last integer is the last stop on the route. However, the stops in between can be visited in any order.



Output
Output r integers on r lines (one integer per line) indicating the distance of each of the r planned routes. If a route is not possible, output 0.



Sample Input
6 3
0 1 2 0 1 1
1 0 1 1 1 0
0 2 0 1 3 0
4 3 1 0 0 0
0 0 1 1 0 0
1 0 0 0 0 0
1 3 5
6 3 2 5
6 1 2 3 4 5

Sample Output
5
0
7

...全文
152 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
liujintaozyc 2009-08-18
no
回复
hyram 2009-08-17
问题描述
苏普利亚岛是一个风景如画的小岛,在岛上只能使用自行车。因此,岛上有许多单程自行车道连接不同的最佳照片拍摄地点。为了帮助旅客计划在岛上的行程,旅游事务委员会要指定r条不同的自行车路线,通过岛上一些最佳照片拍摄地点。基于一张自行车道地图和在三个计划路线中要被包含的最佳照片拍摄地点的列表(为列出的地点不得列入路线) ,请写一个程序确定每条路线,使其距离最小。请注意,每个最佳照片拍摄地点最多只能在路线中出现一次。

输入
有两部分。第一部分提供岛上自行车道的信息。第一行包含两个整数n和r ,n ≤ 100, r ≤ 10 ,表明有N个最佳照片拍摄地点,要设计r条路线。接下来的n行( 第2行到第n+1行)包含n × n个整数( n行每行n个) ,其中i行第j个整数代表从最佳照片拍摄地点i-1到最佳照片拍摄地点j的距离;距离均在0到10的范围内,其中0表示从i-1到j点之间没有单行线.
第二部分输入有r行,代表r条待设计的观光路线。每行列出了路线中要包括的最佳照片拍摄地点。每行的整数代表路线中推荐的最佳照片拍摄地点。第一个整数代表起点最后一个整数代表终点。然而,中间的地点参观顺序任意。

输出
分r行输出r个整数(每行一个整数)代表每条观光路线的距离。如果路线是不可能的,输出0 。
回复
abcde0022 2009-08-17
不会弄
回复
KingWolfOfSky 2009-08-17
顶~~
回复
Felipe_Massa 2009-08-17
原理就是穷举加递归
回复
Felipe_Massa 2009-08-17
int fun(int a[][100],int b[],int n,int m)
{
int i,j,c,ti,t,s;
if(m==2)
s=a[b[0]][b[1]];
else
{
s=020000000000-1;
for (i=1;i<m-1;i++)
{
if(a[b[0]][b[i]]==0)
continue;
ti=a[b[0]][b[i]];
t=b[0];b[0]=b[i];b[i]=b[m-2];b[m-2]=b[m-1];
c=fun(a,b,n,m-1);
b[m-1]=b[m-2];b[m-2]=b[i];b[i]=b[0];b[0]=t;
if(c==0)
continue;
ti+=c;
if(ti<s&&ti!=0)
s=ti;
}
}
if(s!=020000000000-1)
return s;
else
return 0;
}
main()
{ int a[100][100],b[10],c[10][100],i,j,n,r;
while(1)
{
scanf ("%d%d",&n,&r);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for (i=0;i<r;i++)
{
scanf("%d",&b[i]);
for (j=0;j<b[i];j++)
{
scanf("%d",&c[i][j]);
c[i][j]--;
}
}
for (i=0;i<r;i++)
{
b[i]=fun(a,c[i],n,b[i]);
printf("%d\n",b[i]);
}
}
}
回复
hua_zhixing_ 2009-08-16
这道叫什么?
不知道,我懂的意思是不是题目的意思。
是关于图的,用回溯法与动态规划应该都可以吧。
回复
KWHOK 2009-08-16
题目应该翻一下!
回复
fatiezhe 2009-08-16
把题翻译下吧,累
回复
发动态
发帖子
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
社区公告
暂无公告