HDU 1224 Free DIY Tour(spfa求最长路+路径输出)
传送门:
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
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?
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 a blank line between two cases.
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
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1
#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求最长路+路径输出)的更多相关文章
- HDU - 6201 transaction transaction transaction(spfa求最长路)
题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...
- XYZZY(spfa求最长路)
http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others) Memor ...
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6177 Accep ...
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
- hdu 1224 Free DIY Tour(最长的公路/dp)
http://acm.hdu.edu.cn/showproblem.php? pid=1224 基础的求最长路以及记录路径. 感觉dijstra不及spfa好用,wa了两次. #include < ...
- hdu 1534(差分约束+spfa求最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- HDU 1224 Free DIY Tour
题意:给出每个城市interesting的值,和城市之间的飞行路线,求一条闭合路线(从原点出发又回到原点) 使得路线上的interesting的值之和最大 因为要输出路径,所以用pre数组来保存前驱 ...
随机推荐
- MySQL与PHP的连接教程步骤(图文)
本篇文章我们介绍一下PHP与MySQL的整合,既然是与MySQL整合,那么我们首先肯定是要安装MySQL.下面我们就介绍下MySQL的安装方法. 第一步,下载MySQL.下载PHP可以去PHP中文网下 ...
- Class.forName和ClassLoader.loadClass的区别(转载)
Class的装载分了三个阶段,loading,linking和initializing,分别定义在The Java Language Specification的12.2,12.3和12.4.Clas ...
- #include <unistd.h> 的作用
原文:http://blog.csdn.net/ybsun2010/article/details/24832113 由字面意思,unistd.h是unix std的意思,是POSIX标准定义的uni ...
- Android适配--百分比的适配
首先,需要添加com.android.support:percent:24.1.1 包,版本随意. dependencies { compile fileTree(dir: 'libs', inclu ...
- 你真的了解Fragment的生命周期吗?
Android Framwork开发人员中的传奇人物Dianne Hackborn在2010年将Fragment引入了Android,也就是在android3.0之后引入Fragment,他在提交信息 ...
- 使用@Value进行静态常量的值注入
@Component public class ExpressConstant { public static String URL; @Value("${express.url}" ...
- 算法之杨辉三角形(Java语言)
杨辉三角形, 又称贾宪三角形.帕斯卡三角形. 前9层写出来如下: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 ...
- 什么是TTL值?(简单明了的解释)
什么是TTL值? TTL值全称是“生存时间(Time To Live)”,简单的说它表示DNS记录在DNS服务器上的缓存时间. 要理解TTL值,请先看下面的一个例子:假设,有这样一个域名myhost. ...
- PreApplicationStartMethod特性说明
PreApplicationStartMethod主要用于为程序集指定一个方法,在程序集加载时进行一些自定义的初始化处理. 使用方式如下所示,首先在程序集中定义一个公共类型和一个公共静态的方法 然后我 ...
- php中的html元素
我们先看下面的代码 form2.php <html> <head><title>greetins eartyling</title></head& ...