题目描述
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]);
}
}
}