HDU 1224 Free DIY Tour(spfa:最长路径)

Free DIY Tour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7907    Accepted Submission(s): 2588


Problem Description
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?
 

Input
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.
 

Output
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.
 

Sample Input
 
  
2
3
0 70 90
4
1 2
1 3
2 4
3 4
3
0 90 70
4
1 2
1 3
2 4
3 4
 

Sample Output
 
  
CASE 1#
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1

题意:求起点1到终点n+1的最长路径,dis路径要赋负值,所以不能用Dijkstra算法,这里用了spfa;也可以用dp做

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
//const int INF = 0xfffffff;这是可以用for循环赋值的正无穷 
#define inf 0x8fffffff
#define INF 0x3f3f3f3f
int mat[105][105];
int dis[105];         
int vis[105];
int aa[105];
int path[105];
int n,m;
void spfa(int k)
{   
    memset(vis,0,sizeof(vis));//表状态,若入队列,为1,否则为0
    memset(dis,inf,sizeof(dis));//赋值负无穷 
    memset(path,-1,sizeof(path));
    queue<int >q; 
    q.push(k);  //源点入队列 
    vis[k]=1;     //在队列中值为1 
    dis[k]=0;   //源点到源点的路径长度为0 
    while(!q.empty())
    {   
        int m=q.front();     
            vis[m]=0;
        q.pop();
        for(int i=1;i<=n;i++)
        {   
            if(mat[m][i]!=INF)//是通路 
            {   
                if(dis[i]<dis[m]+mat[m][i])    //判断路径权值是否增大            
                {
                    dis[i]=dis[m]+mat[m][i];
					path[i]=m;//记录前驱 
	                 if(!vis[i])       //若减少,且当前元素没在队列中 
	                {   
	                    q.push(i);      //入队列 
	                    vis[i]=1;       //值为1 
	                 
	                }
                }
            }           
        }
    }    
}
void print_path(int u)
{//递归打印路径 
	if(path[u]==-1){//起点 
        printf("1");
        return;
    }
    print_path(path[u]);
    if(u==n) printf("->%d\n",1);//终点,这里根据题意终点变为1 
    else printf("->%d",u);
	
}
int main()
{
	int t;
	scanf("%d",&t);
	int ca=1;
	while(t--)
	{
		memset(mat,INF,sizeof(mat));//赋值正无穷 
		scanf("%d",&n);
		n++;//加1 
		for(int i=1;i<n;i++)
		{
			scanf("%d",&aa[i]);
		}
		aa[n]=0;
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			mat[a][b]=aa[b];
		}
		if(ca!=1) printf("\n");//输出要求 
		printf("CASE %d#\npoints : ",ca++);
		spfa(1);
		printf("%d\n",dis[n]);
		printf("circuit : ");
		print_path(n);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bbhhtt/article/details/80421379