传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1224

Free DIY Tour

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

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

 
Author
JGShining(极光炫影)
 
Source
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1227 1300 1081 1074 1422 
 
分析:
题目意思:
维维要去旅游,然后旅游公司表示有DIY线路,维维可以自己选线路去旅游,每个城市都有一定的兴趣点,维维想要在这次旅游中获得最多的兴趣点数,另外起点在杭州,终点也一定是在杭州,只是中途的城市以及路线要自己DIY,并且绝对不会有从大编号城市到小编号城市的路线(城市编号从1到N,另外N+1表示终点),显示输入测试组数,然后每组测试先是输入一个城市数量N(2<=N<=100,包括起点),然后依次是N个城市的兴趣点数,再输入路线数量,接下来每条路输入两个数A和B,表示有从A到B的路线。最后你要按照格式输出获得的最大兴趣点以及路线就是了。
 
这里的终点为n+1,初始化时不要忘了[n+1]城市。同时输出时应将“n+1”改为1号城市
 
code:
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
using namespace std;
typedef long long LL;
#define max_v 120
int n,m;
int G[max_v][max_v];
int a[max_v];
int dis[max_v];//保存价值
int way[max_v];//保存路
void spfa(int s,int tn)
{
for(int i=;i<=tn;i++)
{
dis[i]=;
way[i]=;
}
queue<int>q;
q.push(s); int p;
while(!q.empty())
{
p=q.front();
q.pop(); for(int i=;i<=tn;i++)
{
if(G[p][i]!=)
{
if(dis[p]+a[i]>dis[i])
{
dis[i]=dis[p]+a[i];
way[i]=p;
q.push(i);
}
}
}
}
}
void pri(int tn)//路径打印
{
int a[];
int c=;
int i=tn;
while(way[i])
{
a[c++]=way[i];
i=way[i];
}
for(int i=c-;i>=;i--)
{
printf("%d->",a[i]);
}
printf("1\n");
}
int main()
{
int t;
scanf("%d",&t);
int k=;
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
a[n+]=;
scanf("%d",&m);
memset(G,,sizeof(G));
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
G[x][y]=;
}
spfa(,n+);
if(k!=)
printf("\n");
printf("CASE %d#\n",k++);
printf("points : %d\n",dis[n+]);
printf("circuit : ");
pri(n+);
}
return ;
}

HDU 1224 Free DIY Tour(spfa求最长路+路径输出)的更多相关文章

  1. HDU - 6201 transaction transaction transaction(spfa求最长路)

    题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...

  2. XYZZY(spfa求最长路)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  3. spfa求最长路

    http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...

  4. POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

  5. 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路

    题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...

  6. hdu 1224 Free DIY Tour(最长的公路/dp)

    http://acm.hdu.edu.cn/showproblem.php? pid=1224 基础的求最长路以及记录路径. 感觉dijstra不及spfa好用,wa了两次. #include < ...

  7. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  8. POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3007   Accep ...

  9. HDU 1224 Free DIY Tour

    题意:给出每个城市interesting的值,和城市之间的飞行路线,求一条闭合路线(从原点出发又回到原点) 使得路线上的interesting的值之和最大 因为要输出路径,所以用pre数组来保存前驱 ...

随机推荐

  1. 浅谈ul布局以及table布局

    我个人对于某些言论说要注重html语义化在布局中的应用,我反而不怎么感冒,试试兼容IE7&&项目期相对较赶的情况下,我还是推荐快速开发为主,兼容性强为主. 如果布局中需要用户边框,推荐 ...

  2. jQuery中判断input的checked属性

    <input type="checkbox" id="ipt1" checked> <input type="checkbox&qu ...

  3. HTML中的嵌入技术

    到目前为止,您应该掌握了将图像\视频和音频嵌入到网页上的诀窍了.此刻,让我们进行深入学习,来看一些能让您在网页中嵌入各种内容类型的元素: <iframe>, <embed> 和 ...

  4. WeinView 与 MITSUBISHI FX 系列 PLC 通讯范例

    1. 范例操作概述 此范例将介绍如何快捷简易地建立WEINVIEW HMI与MITSUBISHI FX系列 PLC通讯. 注意事项:通讯参数设置,通讯线接法. 2. 规划说明 (1) 新建简单 PLC ...

  5. slice()方法 和splice 方法的区别

    定义 splice() 方法 用于插入.删除或替换数组的元素. slice() 方法 可提取字符串的某个部分,并以新的字符串返回被提取的部分. 更多的可查看: http://www.cnblogs.c ...

  6. 微服务&spring cloud架构系列汇总

    为了方便查找,把微服务&微服务架构之spring cloud架构系列文章按时间正序整理了一下,记录如下:   1. 微服务架构之spring cloud 介绍 2. 微服务架构之spring ...

  7. xshell连接虚拟机Connection failed

    一.问题描述:xshell连接不了虚拟机,出现错误提示:Could not connect to '192.168.1.100' (port 22): Connection failed. 二.查找错 ...

  8. Exception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path: 这是 ...

  9. Springmvc和Mybatis中常用的注解

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  10. 毕向东_Java基础视频教程第20天_IO流(11~14)

    第20天-11-IO流(Properties简述) .properties是一种主要在Java相关技术中用来存储应用程序的可配置参数的文件的文件扩展名.它们也可以存储用于国际化和本地化的字符串,这种文 ...