ZOJ_1918 Ferry Loading II(动态规划)

Ferry Loading II

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.

Sample Input

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

题意

一条河,一条船和m个人初始都在河的一边。船每次最多载n个人去河的对岸,需要花费x单位时间,返回同样需要x单位时间。第i个人会在ai时,到达河边。求最快什么时间能讲所有人送到对岸,此时少需要几次载人?

题解:

对所有人按到达时间排序,每次载人优先运送先到达的人。
d p [ i ] dp[i] dp[i]代表第 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)) dp[i]=max(dp[i],max(dp[j],a[i])+2x)(jmax(ix,0)).
因为最后一次无需返回, d p [ n ] − x dp[n]-x dp[n]x即为所求。

#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;
}
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页