【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】

竞赛决不是捷径,它只是另一种艰辛的生活方式。得到与失去,只有时间会去评判;成功与失败,只有历史能去仲裁。我不会永远成功,正如我不会永远失败一样

  C++博客 :: 首页 :: 联系 ::  :: 管理
  6 Posts :: 239 Stories :: 25 Comments :: 0 Trackbacks

常用链接

留言簿(7)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 108476
  • 排名 - 229

最新评论

阅读排行榜

评论排行榜

There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is necessary to find the shortest route which begins and ends at the same place.

在桑给巴尔岛的Adelton城镇上有一个旅游机构。它们决定在提供许多的其它吸引之外,再向客人们提供旅游本镇的服务。 为了从提供的吸引服务中尽可能地获利,这个旅游机构接收了一个精明决定:在相同的起点与终点之间找出一最短路线

Problem:
Your task is to write a program which finds such a route. In the town there are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y_1, ..., y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, the road y_k connects crossing points x_k and x_1. All the numbers x_1,...,x_k should be different. The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L(y_1)+L(y_2)+...+L(y_k) where L(y_i) is the length of the road y_i (1<=i<=k). Your program has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible, because there is no sightseeing route in the town.

你的任务是编写一条程序来找类似的的一条路线。在这个镇上,有N个十字路口(编号1至N),两个十字路口之间可以有多条道路连接,有M条道路(编号为1至M)。但没有一条道路从一个十字路口出发又回到同一个路口。每一条观光路线都是由一 些路组成的,这些道路序号是:y_1, ..., y_k,且k>2。第y_i(1<=i<=k-1)号路是连接第x_i号十字路口和第x_{i+1}号十字路 口的;其中第y_k号路是连接第x_k号十字路口和第x_1号十字路口。而且所有的这些x_1,...,x_k分别代表不同路口的序号。在 某一条观光路线上所有道路的长度的和就是这条观光路线的总长度。换言之L(y_1)+L(y_2)+...+L(y_k) 的和 L(y_i)就是第y_i 号观光路线的长度。你的程序必须找出类似的一条路线:长度必须最小,或者说明在这个城镇上不存在这条观光路线

input:
Input contains a series of tests. The first line of each test contains two positive integers: the number of crossing points N<=100 and the number of roads M<=10000. Each of the next M lines describes one road. It contains 3 positive integers: the number of its first crossing point, the number of the second one, and the length of the road (a positive integer less than 500). Input is ended with a '-1' line.

含几组测试数据。每一组数据的第一行包含两个正整数:十字路口的个数N(N<=100),另一个是道路的 数目M(M<10000)。接下来的每一行描述一条路:每一行有三个正整数:这条路连接的两个路口的编号,以及这条路的长度(小于500的正整数)。最后以`-1`结束

output:
Each line of output is an answer. It contains either a string `No solution.' in case there isn't any sightseeing route, or it contains the numbers of all crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x_1 to x_k from our definition of a sightseeing route), separated by single spaces. If there are multiple sightseeing routes of the minimal length, you can output any one of them.

每一行输出都是一个答案。如果这条观光路线是不存在的话就显示“No solution”;或者输出这条最短路线的经过所有十字 路口的序号,每一个序号之间用一个空格符隔开,这样更能说明这条路线是如何得出来的。也就是用从x_1到x_k描述一条观光路 线。假如满足最短长度的观光路线不止一条,输出其中的任一条即可

input:
5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20
4 3
1 2 10
1 3 20
1 4 30
-1

output:
1 3 5 2
No solution


【参考程序】:
#include<iostream>
using
 namespace std;
int change[101][101],path[101];
int dis[101][101],cost[101][101];
int n,m;
void find_path(int x,int y)
{
    
if (change[x][y]==-1return ;
    find_path(x,change[x][y]);
    path[
++path[0]]=change[x][y];
    find_path(change[x][y],y);
}
int main()
{
    
while (true)
    {
        scanf(
"%d",&n);if (n==-1break;
        scanf(
"%d",&m);
        
for (int i=1;i<=n;i++)
            
for (int j=1;j<=n;j++)
                
if (i==j) cost[i][j]=0;
                
else cost[i][j]=0xFFFFFFF;
        
int a,b,s;
        
for (int i=1;i<=m;i++)
        {
            scanf(
"%d%d%d",&a,&b,&s);
            cost[a][b]
=cost[b][a]=s;
        }
        
for (int i=1;i<=n;i++)
            
for (int j=1;j<=n;j++)
                dis[i][j]
=cost[i][j];
        memset(path,
0,sizeof(path));
        memset(change,
0xFF,sizeof(change));
        
int mins=0xFFFFFFF;
        
for (int k=1;k<=n;k++)
        {
            
for (int i=1;i<k;i++)
                
for (int j=i+1;j<k;j++)
                    
if (mins>dis[i][j]+cost[i][k]+cost[k][j])
                    {
                        mins
=dis[i][j]+cost[i][k]+cost[k][j];
                        path[
0]=2;path[1]=k;path[2]=i;
                        find_path(i,j);
                        path[
++path[0]]=j;
                    }
            
for (int i=1;i<=n;i++)
                
if (i!=k)
                    
for (int j=1;j<=n;j++)
                        
if (j!=&& j!=i)
                            
if (dis[i][j]>dis[i][k]+dis[k][j])
                            {
                                dis[i][j]
=dis[i][k]+dis[k][j];
                                dis[j][i]
=dis[i][j];
                                change[i][j]
=change[j][i]=k;
                            }
        }
        
if (mins==0xFFFFFFF) printf("No solution.\n");
        
else 
        {
            
for (int i=1;i<=path[0]-1;i++) printf("%d ",path[i]);
            printf(
"%d\n",path[path[0]]);
        }
    }
    
return 0;
}
posted on 2009-05-28 14:57 开拓者 阅读(253) 评论(0)  编辑 收藏 引用 所属分类: URAL 题解

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理