zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    剑指offer——最小的K个数和数组中第K大的元素
    Leetcode刷题指南链接整理
    160. Intersection of Two Linked Lists
    100. Same Tree
    92. Reverse Linked List II
    94. Binary Tree Inorder Traversal
    79. Word Search
    78,90,Subsets,46,47,Permutations,39,40 DFS 大合集
    0x16 Tire之最大的异或对
    0x16 Tire
  • 原文地址:https://www.cnblogs.com/rainydays/p/2865978.html
Copyright © 2011-2022 走看看