2013
12-26

# Ticket to Ride

Ticket to Ride is a board game for up to 5 players. The goal of the game is to set up train lines (and to thwart the opponents’ attempts at setting up their train lines). At the beginning of play, each player is assigned four train lines. A player may choose to discard as many of these four assignments as she likes. Each assignment has a score, corresponding to its difficulty (so, typically, a train line between e.g. Stockholm and Tokyo would be worth more than a train line between e.g. Stockholm and Utrecht). At the end of the game, each player gets points for the assignments that they have successfully completed, and penalty points for the assignments that they have failed to complete.

An assignment consists of a pair of cities that are to be connected by a series of shorter railway routes. A route can be claimed (for a certain cost associated with the route), but things are complicated by the fact that there is only a limited number of routes, and once a player claims a route, none of the other players can claim it. A player has successfully set up a train line between two cities if there is a path between the two cities using only routes that have been claimed by this player. For simplicity, we will ignore all additional aspects of the game (including the actual process of claiming routes and additional ways to score points).

For instance, if your assignment is to connect Stockholm and Amsterdam in the Figure above, you would probably want to claim the routes between Stockholm and Copenhagen, and between Copenhagen and Amsterdam. But if another player manages to claim the route between Copenhagen and Stockholm before you, your train line would have to use some other routes, e.g. by going to Copenhagen via Oslo.

In this problem, we will consider the rather bold strategy of trying to complete all four assignments (typically, this will be quite hard). As a preliminary assessment of the difficulty of achieving this, we would like to calculate the minimum cost of setting up all four lines assuming that none of the other players interfere with our plans. Your job is to write a program to determine this minimum cost.

The input consists of several (at most 20) games to be analyzed. Each game starts with two integers 1 <= n <= 30, 0 <= m <= 1 000, giving the number of cities and railway routes in the map, respectively. Then follow n lines, giving the names of the n cities. City names are at most 20 characters long and consist solely of lower case letters (’a’-’z’).

After this follow m lines, each containing the names of two different cities and an integer 1 <= c <= 10 000, indicating that there is a railway route with cost c between the two cities. Note that there may be several railway routes between the same pair of cities. You may assume that it is always possible to set up a train line from any city to any other city.

Finally, there will be four lines, each containing the names of two cities, giving the four train line assignments.

The input is terminated by a case where n = m = 0. This case should not be processed.

The input consists of several (at most 20) games to be analyzed. Each game starts with two integers 1 <= n <= 30, 0 <= m <= 1 000, giving the number of cities and railway routes in the map, respectively. Then follow n lines, giving the names of the n cities. City names are at most 20 characters long and consist solely of lower case letters (’a’-’z’).

After this follow m lines, each containing the names of two different cities and an integer 1 <= c <= 10 000, indicating that there is a railway route with cost c between the two cities. Note that there may be several railway routes between the same pair of cities. You may assume that it is always possible to set up a train line from any city to any other city.

Finally, there will be four lines, each containing the names of two cities, giving the four train line assignments.

The input is terminated by a case where n = m = 0. This case should not be processed.

10 15
stockholm
amsterdam
london
berlin
copenhagen
oslo
helsinki
dublin
reykjavik
brussels
oslo stockholm 415
stockholm helsinki 396
oslo london 1153
oslo copenhagen 485
stockholm copenhagen 522
copenhagen berlin 354
copenhagen amsterdam 622
helsinki berlin 1107
london amsterdam 356
berlin amsterdam 575
london dublin 463
reykjavik dublin 1498
reykjavik oslo 1748
london brussels 318
brussels amsterdam 173
stockholm amsterdam
oslo london
reykjavik dublin
brussels helsinki
2 1
first
second
first second 10
first first
first first
second first
first first
0 0

3907
10

Minimal Steiner Tree是一个NP问题，但可以转成DP来求解。

step1推出：

a = min{dp[m1][i] + dp[m2][i]}，其中m1 | m2 = mask。

step2推出

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
int INF = 99999999, N, K, d[30][30], i, j, k, x, y, z;
int dp[256][30], e[8], v[30], c, b;
string s, t;
while (cin >> N >> K && N)
{
map <string, int> cityMap;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
d[i][j] = i == j ? 0 : INF;
for (i = 0; i < N; i++)
{
cin >> s;
cityMap[s] = i;
}
if (K)
while (cin >> s >> t >> z, x = cityMap[s],
y = cityMap[t],
d[x][y] = d[y][x] = min(d[y][x], z), --K);
for (k = 0; k < N; k++)
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
for (i = 0; i < 8; i++)
{
cin >> s;
e[i] = cityMap[s];
for (j = 0; j < N; j++)
dp[1 << i][j] = d[j][e[i]];
}
for (i = 1; i < 256; i++)
{
if (!(i & (i - 1)))
continue;
// step1
for (k = 0; k < N; k++)
{
dp[i][k] = INF;
v[k] = 0;
/*for (j = 1; j < i; j++)
if ((i | j) == i)
dp[i][k] = min(dp[i][k], dp[j][k] + dp[i-j][k]);*/
for (int sub = i; sub; sub = (sub - 1) & i)
{
dp[i][k] = min(dp[i][k], dp[sub][k] + dp[i - sub][k]);
}
}
// step2
for (j = 0; b = INF, j < N; j++)
{
for (k = 0; k < N; k++)
if (dp[i][k] <= b && !v[k])
b = dp[i][c = k];
for (k = 0, v[c] = 1; k < N; k++)
dp[i][c] = min(dp[i][c], dp[i][k] + d[k][c]);
}
}

// step3
for (i = 0, b = INF; z = 0, i < 256; b = min(b, z), i++)
for (j = 0; y = 0, j < 4; z += !!y * dp[y][x], j++)
for (k = 0; k < 8; k += 2)
if ((i >> k & 3) == j)
y += 3 << k, x = e[k];

cout << b << endl;
}
return 0;
}

1. #include <stdio.h>
int main(void)
{
int arr[] = {10,20,30,40,50,60};
int *p=arr;
printf("%d,%d,",*p++,*++p);
printf("%d,%d,%d",*p,*p++,*++p);
return 0;
}

为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下？