Free DIY Tour(HDU 1224)---最长路

本文介绍了一种使用SPFA算法解决特定条件下的最长路问题的方法。在给定的城市地图上,每个城市都有一个点数,从指定城市出发,只能从编号小的城市飞往编号大的城市,最终返回起点。文章详细解释了如何利用SPFA算法找到最大点数和的路径。

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

题目链接

题目描述

Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around the world. It’s a good chance to relax themselves. To most of them, it’s the first time to go abroad so they decide to make a collective tour.
The tour company shows them a new kind of tour circuit - DIY circuit. Each circuit contains some cities which can be selected by tourists themselves. According to the company’s statistic, each city has its own interesting point. For instance, Paris has its interesting point of 90, New York has its interesting point of 70, ect. Not any two cities in the world have straight flight so the tour company provide a map to tell its tourists whether they can got a straight flight between any two cities on the map. In order to fly back, the company has made it impossible to make a circle-flight on the half way, using the cities on the map. That is, they marked each city on the map with one number, a city with higher number has no straight flight to a city with lower number.
Note: Weiwei always starts from Hangzhou(in this problem, we assume Hangzhou is always the first city and also the last city, so we mark Hangzhou both 1 and N+1), and its interesting point is always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If you were Weiwei, how did you DIY it?

输入格式

The input will contain several cases. The first line is an integer T which suggests the number of cases. Then T cases follows.
Each case will begin with an integer N(2 ≤ N ≤ 100) which is the number of cities on the map.
Then N integers follows, representing the interesting point list of the cities.
And then it is an integer M followed by M pairs of integers [Ai, Bi] (1 ≤ i ≤ M). Each pair of [Ai, Bi] indicates that a straight flight is available from City Ai to City Bi.

输出格式

For each case, your task is to output the maximal summation of interesting points Weiwei and his fellow workers can get through optimal DIYing and the optimal circuit. The format is as the sample. You may assume that there is only one optimal circuit.
Output a blank line between two cases.

输入样例

2
3
0 70 90
4
1 2
1 3
2 4
3 4

输出样例

CASE 1#
points : 90
circuit : 1->3->1

分析

题目大意有N个城市,每个城市都有一个点数,从编号1的城市开始飞,每次只能从编号小的飞到编号大的城市,最后飞回起点城市(此时起点编号为N+1),求最大点数和。
显然这是一个最长路问题,由于题目限定编号大的不能飞到编号小的城市,所以题目肯定不存在环,对于最长路问题,dijkstra算法就不适用了,此时用SPFA算法求解。

源程序

SPFA算法

#include <bits/stdc++.h>
#define MAXN 105
using namespace std;
struct Edge{	//链式前向星 
	int v,w,next;
	Edge(){};
	Edge(int _v,int _w,int _next){
		v=_v,w=_w,next=_next;
	};
}edge[MAXN*MAXN];
int EdgeCount,head[MAXN];
int t,n,m,cnt,point[MAXN],dis[MAXN],pre[MAXN],path[MAXN];
bool ven[MAXN];
void addEdge(int u,int v,int w)	//链式前向星建图 
{
	edge[++EdgeCount]=Edge(v,w,head[u]);
	head[u]=EdgeCount;
}
void SPFA()
{
	queue<int> q;
	memset(dis,0,sizeof(dis));
	memset(ven,false,sizeof(ven));
	memset(pre,-1,sizeof(pre));
	dis[1]=0;
	q.push(1);
	while(!q.empty()){
		int u=q.front();q.pop();
		ven[u]=false;
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v,w=edge[i].w;
			if(dis[v]<dis[u]+w){	//最长路
				dis[v]=dis[u]+w;
				pre[v]=u;
				if(!ven[v]){
					q.push(v);
					ven[v]=true;
				}
			}
		}
	}
}
int main()
{
	scanf("%d",&t);	//测试组数 
	for(int cas=1;cas<=t;cas++){
		memset(head,0,sizeof(head));	//初始化 
		EdgeCount=0;
		scanf("%d",&n);	 
		for(int i=1;i<=n;i++)scanf("%d",&point[i]);	//读入数据 
		point[n+1]=point[1]; 
		scanf("%d",&m);
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			if(u==v)continue;	//自环 
			else if(u<v)addEdge(u,v,point[v]);	//将点数转成边权重 
			else addEdge(v,u,point[u]); 
		} 
		SPFA();
		cnt=0;	//经过城市数 
		int i=n+1; 
		while(i!=-1)path[++cnt]=i,i=pre[i];	//存储路径 
		path[1]=1;	//将n+1号城市改为1号城市
		if(cas!=1)printf("\n");	//空行
		printf("CASE %d#\n",cas);
		printf("points : %d\n",dis[n+1]);
		printf("circuit : ");
		for(int i=cnt;i>=1;i--){
			if(i!=1)printf("%d->",path[i]);
			else printf("%d\n",path[i]);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值