UVA 590 - Always on the run

第一次把题意理解错了,第二次因为一个 ] ,wrong了四次,还好在极值样例的检测下,找出来了。以后打代码一定要注意了。

我是这样定义状态的,f[i][j]表示在第i天时,在城市j所花费的最小费用,很简单的dp题:

代码如下:

#include<stdio.h>
#include<string.h>
#define INF 2000000000
long long b[15][15][35];
long long f[1010][15];
int n, k, num;
void output()
{
    if(f[k+1][n] < INF)printf("The best flight costs %lld.\n",f[k+1][n]);
    else printf("No flight possible.\n");
    printf("\n");
}
void solve()
{
    for(int i = 2; i <= n; i ++)
    f[1][i] = INF;
    f[1][1] = 0;
    for(int i = 2; i <= k+1; i ++)
        for(int j = 1; j <= n; j ++)
        {
            f[i][j] = INF;
            for(int g = 1; g <= n; g ++)
                if(g != j && f[i-1][g] != INF)
                {
                    if((i-1)%b[g][j][0]&&f[i-1][g]+b[g][j][(i-1)%b[g][j][0]] < f[i][j])
                        f[i][j] = f[i-1][g] + b[g][j][(i-1)%b[g][j][0]];
                    if(!((i-1)%b[g][j][0]) && f[i-1][g] + b[g][j][b[g][j][0]] < f[i][j])
                        f[i][j] = f[i-1][g] + b[g][j][b[g][j][0]];
                }
    }
    output();
}
void input()
{
    while(scanf("%d%d",&n, &k) == 2)
    {
        if(n==0&&k==0) break;
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                if(i != j)
                {
                    scanf("%d",&b[i][j][0]);
                    for(int g = 1; g <= b[i][j][0]; g ++)
                    {
                        scanf("%d",&b[i][j][g]);
                        if(b[i][j][g] == 0) b[i][j][g] = INF;
                    }
                }
        printf("Scenario #%d\n",++num);
        solve();
    }
}
int main()
{
    num = 0;
    input();
    return 0;
}
posted on 2012-04-11 16:55  BFP  阅读(396)  评论(0编辑  收藏  举报