上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助。今天在这里和大家一起学习一下nullnull
1
#include<stdio.h>
const int Max_City=200;
const int Max_Ticket=20;
const int Max_TicketLength=10;
const int Max_TripLength=10;
struct info
{
int cost,n;
int city[Max_TicketLength];
};
int cases,NT,NI,i,j,k,l,tmp,trip,change,sum,n;
int city[Max_City];
int list[Max_TripLength];
info ticket[Max_Ticket];
int f[Max_TripLength][Max_City][4];
int route[Max_TripLength * Max_City];
int find(int x)
{
int i;
for(i=0;i<sum;i++)
if(city[i]==x)
return i;
city[sum]=x;
sum++;
return (sum-1);
}
int main()
{
cases=0;
while(1)
{
scanf("%d",&NT);
sum=0;
if(NT==0)
break;
else
cases++;
for(i=0;i<NT;i++)
{
scanf("%d %d",&ticket[i].cost,&ticket[i].n);
for(j=0;j<ticket[i].n;j++)
{
scanf("%d",&tmp);
ticket[i].city[j]=find(tmp);
}
}
scanf("%d",&NI);
for(trip=0;trip<NI;trip++)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&tmp);
list[i]=find(tmp);
}
for(i=0;i<n;i++)
for(j=0;j<sum;j++)
f[i][j][0]=-1;
for(i=0;i<4;i++)
f[0][list[0]][i]=0;
for(i=0;i<n;i++)
{
while(1)
{
change=0;
for(j=0;j<sum;j++) if(f[i][j][0]>=0)
for(k=0;k<NT;k++) if(ticket[k].city[0]==j)
{
tmp=i+1;
for(l=1;l<ticket[k].n;l++)
{
if(tmp<n&&list[tmp]==ticket[k].city[l])
tmp++;
if(f[tmp-1][ticket[k].city[l]][0]<0 || f[tmp-1][ticket[k].city[l]][0]>f[i][j][0]+ticket[k].cost)
{
f[tmp-1][ticket[k].city[l]][0]=f[i][j][0]+ticket[k].cost;
f[tmp-1][ticket[k].city[l]][1]=k;
f[tmp-1][ticket[k].city[l]][2]=i;
f[tmp-1][ticket[k].city[l]][3]=j;
change++;
}
}
}
if(change==0)
break;
}
}
printf("Case %d, Trip %d: Cost = %d\n",cases,trip+1,f[n-1][list[n-1]][0]);
l=0;
i=n-1;j=list[n-1];
while(i||j!=list[0])
{
route[l]=f[i][j][1];
k=f[i][j][2];
j=f[i][j][3];
i=k;
l++;
}
printf(" Tickets used:");
for(i=l-1;i>=0;i--)
printf(" %d",route[i]+1);
printf("\n");
}
}
return 0;
}
每日一道理
听,是谁的琴声,如此凄凉,低调的音,缓慢的节奏,仿佛正诉说着什么。音低调得略微有些抖动,听起来似乎心也有些抖动,我感觉到一种压抑的沉闷气息,是否已凝结在这空气中……
听,是谁的琴声,如此凄凉,低调的音,缓慢的节奏,仿佛正诉说着什么。音低调得略微有些抖动,听起来似乎心也有些抖动,我感觉到一种压抑的沉闷气息,是否已凝结在这空气中……
2
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int profit[55][55],pies[55],ices[55],pies_left[55],ices_left[55],profit_p[55],profit_i[55],
combins[55][55],from_p[55],from_i[55],n,m,i,j,k,new_profit,sum,cases,inf,updated;
double total_profit,x;
void go()
{
memset(combins,0,sizeof(combins));
total_profit=0;
for(i=0;i<sum;i++)
{
for(j=0;j<m;j++)
profit_i[j]=inf;
for(j=0;j<n;j++)
{
from_p[j]=-1;
profit_p[j]=inf;
if(pies_left[j])
profit_p[j]=0;
}
updated=1;
while(updated)
{
updated=0;
for(j=0;j<n;j++) if(profit_p[j]>inf)
for(k=0;k<m;k++) if(profit[j][k]>0)
{
new_profit=profit_p[j]+profit[j][k];
if(profit_i[k]==inf||new_profit<profit_i[k])
{
profit_i[k]=new_profit;
from_i[k]=j;
updated=1;
}
}
for(j=0;j<m;j++) if(profit_i[j]>inf)
for(k=0;k<n;k++) if(combins[k][j])
{
new_profit=profit_i[j]-profit[k][j];
if(profit_p[k]==inf||new_profit<profit_p[k])
{
profit_p[k]=new_profit;
from_p[k]=j;
updated=1;
}
}
}
k=0;
for(j=1;j<m;j++)
if(profit_i[k]==inf||!ices_left[k]||profit_i[j]<profit_i[k]&&ices_left[j])
k=j;
ices_left[k]--;
total_profit+=profit_i[k];
while(1)
{
j=from_i[k];
combins[j][k]++;
if(from_p[j]<0)
break;
k=from_p[j];
combins[j][k]--;
}
pies_left[j]--;
}
}
int main()
{
inf=-(1<<30);
while(scanf("%d%d",&n,&m)&&n)
{
sum=0;
for(i=0;i<n;i++)
{
scanf("%d",&pies[i]);
pies_left[i]=pies[i];
sum+=pies_left[i];
}
for(i=0;i<m;i++)
{
scanf("%d",&ices[i]);
ices_left[i]=ices[i];
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
scanf("%lf",&x);
profit[i][j]=int(x*100+0.1);
}
go();
printf("Problem %d: %.2lf to ",++cases,total_profit/1e2);
memcpy(pies_left,pies,sizeof(pies));
memcpy(ices_left,ices,sizeof(ices));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(profit[i][j]>0)
profit[i][j]=1001-profit[i][j];
go();
printf("%.2lf\n",(sum*1001-total_profit)/1e2);
}
return 0;
}
文章结束给大家分享下程序员的一些笑话语录: 问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?
答:因为李艳红这种败类,所以我没女友!