Time Limit: 2000 ms
Memory Limit: 65536 KB

#### Problem Description

Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river’s current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.

There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?

#### Input

The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.

#### Output

For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time.

You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.

2
2 10 10
0
10
20
30
40
50
60
70
80
90
2 10 3
10
30
40

#### Sample Output

100 5
50 2

###### 题解：

d p [ i ] dp[i] 代表第 i i 个人及其之前的人都被运送到对岸，且船回到原地的最短时间，则有
d p [ i ] = m a x ( d p [ i ] , m a x ( d p [ j ] , a [ i ] ) + 2 ∗ x ) ( j ≥ m a x ( i − x , 0 ) ) dp[i] = max(dp[i], max(dp[j],a[i]) + 2*x) (j\ge max(i-x, 0)) .

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6

using namespace std;
typedef long long LL;
typedef pair<LL, int> P;
const int maxn = 20;
const int mod = 1000000007;
int tmp, in[maxn], vis[maxn], a[maxn][maxn];
vector<int> g;
void floyd(int n);
void dfs(int num, int ans);

int main()
{
int n, m, i, j, k, ans;
while(scanf("%d", &n), n)
{
ans = 0;
tmp = INF;
g.clear();
memset(vis, 0, sizeof(vis));
memset(in, 0, sizeof(in));
for(i=1;i<=n;i++){
a[i][i] = 0;
for(j=i+1;j<=n;j++)
a[i][j] = a[j][i] = 10000000;
}
scanf("%d", &m);
for(i=0;i<m;i++){
int w;
scanf("%d %d %d", &j, &k, &w);
a[j][k] = a[k][j] = min(w, a[j][k]);
ans += w;
in[j]++, in[k]++;
}
floyd(n);
for(i=1;i<=n;i++)
if(in[i]%2)g.push_back(i);
dfs(0, 0);
printf("%d\n", ans+tmp);
}
return 0;
}

void floyd(int n)
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j] = min(a[i][j], a[i][k]+a[k][j]);
}

void dfs(int num, int ans)
{
if(num == g.size()){
tmp = min(tmp, ans);
return;
}
int i, j, u, v;
for(int i=0;i<g.size();i++){
if(!vis[g[i]]){
u = g[i];break;
}
}
vis[u] = 1;
for(i=0;i<g.size();i++)
if(!vis[g[i]]){
vis[g[i]] = 1;
dfs(num+2, ans+a[u][g[i]]);
vis[g[i]] = 0;
}
vis[u] = 0;
}

10-23 1828
10-06 459

09-12 886
05-03 897
04-14 684
10-04 1066
10-03 467
07-23 930
08-25 654