题目链接: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 ≤ i, j ≤ 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; }