比赛 |
NOIP_5 |
评测结果 |
AAAAAAAAAA |
题目名称 |
Perform巡回演出 |
最终得分 |
100 |
用户昵称 |
zqzas |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2008-09-24 22:49:14 |
显示代码纯文本
#include <stdio.h>
#include <string.h>
#define maxn 12
#define maxm 1010
#define inf 999999
//n: city
//m: day
int n,m,ans,d[maxn][maxn],cost[maxn][maxn][35],f[maxm][maxn];
FILE *f1,*f2;
void search(int day,int city)
{
int i,p;
if (day==m+1)
{
if (city==n)
{
f[day][city]=0;
}
else
f[day][city]=inf;
return;
}
f[day][city]=inf;
for (i=1;i<=n;i++)
{
if (i==city)
continue;
p=day%d[city][i];
if (f[day+1][i]==-1 && cost[city][i][p]!=inf)
search(day+1,i);
if (p==0)
p=d[city][i];
if (cost[city][i][p]!=inf)
if (f[day+1][i]+cost[city][i][p]<f[day][city])
f[day][city]=f[day+1][i]+cost[city][i][p];
}
}
void run(void)
{
search(1,1);
}
void ini(void)
{
int k,i,j;
do
{
fscanf(f1,"%d%d",&n,&m);
memset(d,0,sizeof(d));
memset(cost,0,sizeof(cost));
for (i=0;i<=m;i++)
for (j=0;j<=n;j++)
f[i][j]=-1;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
if (i==j)
continue;
fscanf(f1,"%d",&d[i][j]);
for (k=1;k<=d[i][j];k++)
{
fscanf(f1,"%d",&cost[i][j][k]);
if (cost[i][j][k]==0)
cost[i][j][k]=inf;
}
cost[i][j][0]=cost[i][j][d[i][j]];
}
for (i=0;j<=m;j++)
f[m][n]=inf;
run();
ans=f[1][1];
if (ans==inf)
ans=0;
if (!(n==0 && m==0))
fprintf(f2,"%d",ans);
if (!(n==0 && m==0))
fprintf(f2,"\n");
}while (n!=0 || m!=0);
}
int main(void)
{
f1=fopen("candy.in","r");
f2=fopen("candy.out","w");
ini();
fclose(f1);fclose(f2);
return 0;
}