1.题目描述:
Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6695 Accepted Submission(s): 2183
Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around the world. It's a good chance to relax themselves. To most of them, it's the first time to go abroad so they decide to make a collective tour.
The tour company shows them a new kind of tour circuit - DIY circuit. Each circuit contains some cities which can be selected by tourists themselves. According to the company's statistic, each city has its own interesting point. For instance, Paris has its interesting point of 90, New York has its interesting point of 70, ect. Not any two cities in the world have straight flight so the tour company provide a map to tell its tourists whether they can got a straight flight between any two cities on the map. In order to fly back, the company has made it impossible to make a circle-flight on the half way, using the cities on the map. That is, they marked each city on the map with one number, a city with higher number has no straight flight to a city with lower number.
Note: Weiwei always starts from Hangzhou(in this problem, we assume Hangzhou is always the first city and also the last city, so we mark Hangzhou both 1 and N+1), and its interesting point is always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If you were Weiwei, how did you DIY it?
Input The input will contain several cases. The first line is an integer T which suggests the number of cases. Then T cases follows.
Each case will begin with an integer N(2 ≤ N ≤ 100) which is the number of cities on the map.
Then N integers follows, representing the interesting point list of the cities.
And then it is an integer M followed by M pairs of integers [Ai, Bi] (1 ≤ i ≤ M). Each pair of [Ai, Bi] indicates that a straight flight is available from City Ai to City Bi.
Output For each case, your task is to output the maximal summation of interesting points Weiwei and his fellow workers can get through optimal DIYing and the optimal circuit. The format is as the sample. You may assume that there is only one optimal circuit.
Output a blank line between two cases.
Sample Input
2
3
0 70 90
4
1 2
1 3
2 4
3 4
3
0 90 70
4
1 2
1 3
2 4
3 4
Sample Output
CASE 1#
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1
Author JGShining(极光炫影)
Source 杭州电子科技大学第三届程序设计大赛
Recommend Ignatius.L | We have carefully selected several similar problems for you: 1227 1080 1300 1158 1074
2.题意概述:
有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度。
如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费)。
现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和。
求最小花费,如果有多条路经符合,则输出字典序最小的路径。
3.解题思路:①这题的关键在于按照字典序输出路径。
假设有
1--->2 2
2--->3 1
1--->3 3
求1到3的最小花费路径.
那么显然后两条路:
1-->3 3
1-->2-->3 3
它们的花费是相同的,但是路径的字典序不同,“123”比“13”字典序要小,所以应当输出1-->2-->3.
②用一个数组pre记录每一点的上一个结点。按照一般的单源最短路算法,在松弛时是有“小于”就直接松弛, 而这题还要判断“等于”的情况,在“等于”之时,这是要选择哪一个父结点所形成的路径字典序较小,就选择哪一个父结点。
所以,在“等于”之时,可以求出原先的路径, 再求出当前这个的路径,把路径转化成字符串,然后直接比较大小决定是否要换父结点。
③求路径的方法并转化为字符串的方法, 其实很简单,用一个3行的递归函数就解决了。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 111
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7
using namespace std;
typedef long long ll;
int mp[maxn][maxn], dis[maxn][maxn], pre[maxn][maxn], cost[maxn];
void floyd(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
dis[i][j] = mp[i][j];
pre[i][j] = j;
}
}
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (dis[i][j] > dis[i][k] + dis[k][j] + cost[k])
{
dis[i][j] = dis[i][k] + dis[k][j] + cost[k];
pre[i][j] = pre[i][k];
}
else if (dis[i][j] == dis[i][k] + dis[k][j] + cost[k])
{
if (pre[i][j] > pre[i][k] && i != k)
pre[i][j] = pre[i][k];
}
}
}
}
}
void print(int i, int j)
{
printf("From %d to %d :\n", i, j);
printf("Path: ");
int k = i;
printf("%d", i);
while (k != j)
{
printf("-->%d", pre[k][j]);
k = pre[k][j];
}
puts("");
printf("Total cost : %d\n\n", dis[i][j]);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n;
while (~scanf("%d", &n), n)
{
/*init*/
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
mp[i][j] = INF;
mp[i][i] = 0;
}
/*read*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
int tmp;
scanf("%d", &tmp);
if (tmp != -1)
mp[i][j] = tmp;
}
for (int i = 1; i <= n; i++)
scanf("%d", &cost[i]);
floyd(n);
/*print*/
int sta, ed;
while (~scanf("%d%d", &sta, &ed), sta != -1, ed != -1)
print(sta, ed);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}