poj1476

递推

View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define maxn 15
#define maxk 1005
#define maxd 35
#define inf 0x3f3f3f3f

int n, m;
int f[maxn][maxk];
int flight_num[maxn][maxn];
int flight[maxn][maxn][maxd];

void input()
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i != j)
            {
                scanf("%d", &flight_num[i][j]);
                for (int k = 0; k < flight_num[i][j]; k++)
                    scanf("%d", &flight[i][j][k]);
            }
}

void cal(int day, int u, int v)
{
    if (f[u][day] == -1)
        return;
    if (flight_num[u][v] == 0)
        return;
    if (flight[u][v][day % flight_num[u][v]] != 0
            && (f[v][day + 1] == -1 || f[v][day + 1] > f[u][day] + flight[u][v][day % flight_num[u][v]]))
        f[v][day + 1] = f[u][day] + flight[u][v][day % flight_num[u][v]];
}

int work()
{
    memset(f, -1, sizeof(f));
    f[0][0] = 0;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            for (int k = 0; k < n; k++)
                if (j != k)
                    cal(i, j, k);
    return f[n - 1][m];
}

void output()
{
    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j < n; j++)
            printf("%d ", f[j][i]);
        puts("");
    }
}

int main()
{
//    freopen("t.txt", "r", stdin);
    int t = 0;
    while (scanf("%d%d", &n, &m), n | m)
    {
        input();
        int ans = work();
//        output();
        printf("Scenario #%d\n", ++t);
        if (ans == -1)
            puts("No flight possible.");
        else
            printf("The best flight costs %d.\n", ans);
        puts("");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值