URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005

2005. Taxi for Programmers

Time limit: 0.5 second

Memory limit: 64 MB

The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their training. The exhausted students gloomily leave their computers. But there’s
something that cheers them up: Misha, the kind coach is ready to give all of them a lift home in his brand new car. Fortunately, there are no traffic jams on the road. Unfortunately, all students live in different districts. As Misha is a programmer, he highlighted
and indexed five key points on the map of Yekaterinburg: the practice room (1), Kirill’s home (2), Ilya’s home (3), Pasha’s and Oleg’s home (4; they live close to each other) and his own home (5). Now he has to find the optimal path. The path should start
at point 1, end at point 5 and have minimum length. Moreover, Ilya doesn’t want to be the last student to get home, so point 3 can’t be fourth in the path.

Input

The input contains a table of distances between the key points. It has five rows and five columns. The number in the i’th row and the j’th column (1 ≤ ij ≤ 5) is
a distance between points i andj. All distances are non-negative integers not exceeding 10 000. It is guaranteed that the distance from any point to itself equals 0, and for any two points, the distance between them is the same in both directions.
It is also guaranteed that the distance between any two points doesn’t exceed the total distance between them through another point.

Output

Output two lines. The first line should contain the length of the optimal path. The second line should contain five space-separated integers that are the numbers of the points in the order Misha should
visit them. If the problem has several optimal solutions, you may output any of them.

Sample

input output
0 2600 3800 2600 2500
2600 0 5300 3900 4400
3800 5300 0 1900 4500
2600 3900 1900 0 3700
2500 4400 4500 3700 0
13500
1 2 3 4 5

Problem Author: Mikhail Rubinchik. (Prepared by Oleg Merkurev)

Problem Source: Ural Regional School Programming Contest 2013

题意:

找最短路!且第3点不能在第四条路径!

代码如下:

#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
int a[7][7];
//void Floyd()
//{
//    for(int k = 1; k <= 5; k++)
//    {
//        for(int i = 1; i <= 5; i++)
//        {
//            for(int j = 1; j <= 5; j++)
//            {
//                if(a[i][k]+a[k][j] < a[i][j])
//                {
//                    a[i][j] = a[i][k]+a[k][j];
//                }
//            }
//        }
//    }
//}
int main()
{
    for(int i = 1; i <= 5; i++)
    {
        for(int j = 1; j <= 5; j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
//    Floyd();
    int ans = 0;
    int r[7];
    int minn = INF;
    for(int i = 2; i <= 4; i++)//2
    {
        for(int j = 2; j <= 4; j++)//3
        {
            for(int k = 2; k <= 4; k++)//4
            {
                if(i!=j && i!=k && j!= k && k!=3)
                {
                    int tt = a[1][i]+a[i][j]+a[j][k]+a[k][5];
                    if(tt < minn)
                    {
                        minn = tt;
                        r[2] = i, r[3] = j, r[4] = k;
                    }
                }
            }
        }
    }
    printf("%d\n",minn);
    printf("1 %d %d %d 5\n",r[2],r[3],r[4]);
    return 0;
}
时间: 2024-12-23 05:48:57

URAL 2005. Taxi for Programmers (最短路 数学啊)的相关文章

Timus 2005. Taxi for Programmers 题解

The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their training. The exhausted students gloomily leave their computers. But there's something that cheers them up: Misha, the kind coach

URAL 1934 Black Spot --- 简单最短路变形

边权为1,在维护最短路的同时维护p值最小,我直接存的(1-p),即不遇见的概率,要使得这个值最大. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #includ

URAL 1727. Znaika&#39;s Magic Numbers(数学 vector)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

URAL 1056 Computer Net(最短路)

Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecutive computer plug-up to one that has already been connected to the net. Each new computer gets an ordinal number, but the protocol contains the numbe

ural 2029 Towers of Hanoi Strike Back (数学找规律)

ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺塔问题,给定一串只有(A, B, C)的字符串(A代表在第一根柱子,B代表在第二根柱子,C代表在第三根柱子),从前往后代表盘子的大小,第 i 个字母代表di i 个盘子在某个柱子上.问移动给定字母状态的盘子最少需要多少步. 思路:首先,从后往前看(最大的盘子),如果不在当前柱子上,那么移动到目标柱子需要 2

ural 2032 Conspiracy Theory and Rebranding (数学水题)

ural 2032  Conspiracy Theory and Rebranding 链接:http://acm.timus.ru/problem.aspx?space=1&num=2032 题意:给定一个三角形的三条边 (a, b, c),问是否可放在二维坐标,使得3个顶点都是整数点.若可以,输出任意一组解,否则,输出 -1. 思路:暴力枚举:以 a 为半径做第一象限的 1/4 圆, 以 b 为半径做 一.四 象限的半圆,存储整数点的解,暴力枚举 a 整数点与 b 整数点是否构成长度为 c

URAL 1607. Taxi

1607. Taxi Time limit: 0.5 second Memory limit: 64 MB Petr likes going by taxi. For him, it is not only the pleasure of a fast and comfortable ride, but also the opportunity to bargain with the driver over the fare. The bargaining between Petr and ta

URAL 1727. Znaika&amp;#39;s Magic Numbers(数学 vector)

主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

【分享】近4000份数学学习资源免费分享给大家

一直以来喜欢收集数学类的教程资源,于是费了好大劲从万千合集站上扒拉了下来,总结归类了一下,一共有将近4000本电子书.经测试,均可免费下载,可能会弹出小广告,可不必理会之.[仅供学术学习和交流,请无用于商业用途.]另外,如有可能,还请尽量支持正版纸质书.   数学史(54)     数学史.rar 55.6 MB   数学的起源与发展.rar 4.3 MB   费马大定理—一个困惑了世间智者358年的谜.pdf 9.5 MB   通俗数学名著译丛14-无穷之旅:关于无穷大的文化史.pdf 14.