c语言noip试题,noip模拟题 收费站(cost)C/C++代码在某个遥远的国家里,有n个城市。编号为1,2,3,……,n,并有m条双向的公路。每条公路连接着两个城市。开车每经过一个城市,都会被收...

该博客主要介绍了一个城市路线搜索算法的实现,包括数据结构`SearchStep`和`Road`的定义,以及`FindNextCity`和`DoSearch`两个关键函数。算法用于寻找从起点到终点的最低费用路径,同时考虑了汽车油箱容量的限制。在主函数中,首先获取用户输入的城市数量、线路信息、出发城市、目标城市和油箱容量,然后随机生成城市收费和线路信息,最后执行搜索算法并输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include //记录每一步的选择。

struct SearchStep

{

int city;

int road;

int s;

SearchStep* next;

SearchStep* pre;

};

struct Road

{

int startc;

int endc;

int ci;

};

int FindNextCity(SearchStep* as,Road* ar,int n)

{

//查找线路是否有一端符合当前城市

int icity;

SearchStep* ps;

if (ar->startc == n)

icity = ar->endc ;

else if (ar->endc == n)

icity = ar->startc ;

else

return 0;

//查找线路是否已经过另一端城市

ps = as;

while (ps != NULL)

{

if (ps->city == icity)

return 0;

ps = ps->next ;

}

return icity;

}

int DoSearch(SearchStep* as,int aa,Road* ar,int* ac,int* ap,int ad)

{

//as 搜寻的路径;

//aa 公路数,ar 公路信息

//ac 最低的最高线路途经城市费用

//ap 城市收费信息

//ad 目标城市

int m,nextcity,nexts,ipay,imax;

SearchStep *ps,*pp;

ps = as;

if (ps->city == ad)

{

//查找所经过的路径,设置最大缴费值,比较缴费值和起讫城市的收费值,如果和其中之一相等,则直接返回

pp = ps;

while (pp != NULL)

{

ipay = *(ap + pp->city -1);

if (imax < ipay)

imax = ipay;

pp = pp->pre ;

}

*ac = imax;

if ((imax == *(ap + as->city -1)) || (imax == *(ap + ps->city -1)))

return 1;

else

{

if (ps->pre != NULL)

{

ps = ps->pre ;

ps ->next = NULL;

}

return 0;

}

}

m = ps->road;

while (m < aa)

{

nextcity = FindNextCity(as,ar + m,ps->city);

if (nextcity > 0)

{

//执行到下一步

if (*ac > 0)

if (*(ap + nextcity -1) > *ac)

{

//如果所到城市的费用大于已有线路最大费用,跳过

m++;

continue;

}

if (ps->s - (ar + m)->ci > 0)

{

ps->next = new SearchStep;

ps->road = m;

ps = ps->next;

ps->next =NULL;

ps->pre =as;

ps->city = nextcity;

ps->road = 0;

ps->s = as->s - (ar + m)->ci;

nexts = DoSearch(ps,aa,ar,ac,ap,ad);

if (nexts == 1) return nexts;

}

}

m++;

}

if (ps->pre != NULL)

{

ps = ps->pre ;

ps ->next = NULL;

}

return 0;

}

int main(int argc, char* argv[])

{

//第一步,录入城市数量,线路数量,出发城市,目标城市,汽车油箱容量

int ccount,rcount,startc,endc,vehs,imaxcost;

int inputright;

inputright = 0;

while (inputright == 0)

{

cout << "请输入城市数量,线路数量,出发城市,目标城市,汽车油箱容量\n";

cin >> ccount >> rcount >> startc >> endc >> vehs;

if (startc > ccount || endc > ccount)

cout << "出发城市或目标城市不能大于城市数,请重新输入!";

else if (ccount > 10000)

cout << "城市数量不能大于10000,请重新输入!";

else if (rcount > 50000)

cout << "线路数量不能大于50000,请重新输入!";

else

inputright ++;

}

//初始化城市收费,线路信息

int *Pcity = new int [ccount];

Road *Proad = new Road [rcount];

int i,num1,num2,num3;

srand((unsigned)time(NULL));

for (i = 0;i {

num1 = rand()%10000 + 1;

*(Pcity + i) = num1;

}

for (i = 0; i {

num1 = rand()%ccount + 1;

num2 = rand()%ccount + 1;

if (num1 == num2)

{

num2 = (num1 + 1) % ccount + 1;

}

(Proad + i)->startc = num1;

(Proad + i)->endc = num2;

num3 = rand()%10000 + 1;

(Proad + i)->ci = num3;

}

//输出题目的数据

cout << "城市数量\t线路数量\t出发城市\t目标城市\t汽车油箱容量\n";

cout << ccount << "\t" << rcount << "\t" ;

cout << startc << "\t" << endc << "\t" ;

cout << vehs << "\n";

for (i = 0;i cout << *(Pcity + i) << "\n";

for (i = 0;i cout << (Proad + i)->startc << "\t"<< (Proad + i)->endc << "\t"

// getchar();

//开始寻找线路

SearchStep sFirst;

sFirst.next = NULL;

sFirst.city = startc;

sFirst.pre = NULL;

sFirst.road = 0;

sFirst.s = vehs;

imaxcost = -1;

DoSearch(&sFirst,rcount,Proad,&imaxcost,Pcity,endc);

//输出结果

cout << "搜索结果为:" << imaxcost << endl;

getchar();

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值