Floyd算法并输出路径
Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3656 Accepted Submission(s): 1180
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 andN+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.
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
CASE 1#
points : 90
circuit : 1->3->1 CASE 2#
points : 90
circuit : 1->2->1
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#define M 40
#define eps 1e-10
#define inf 99999999
#define mod 1000000000
using namespace std;
int n,path[111][111],dis[111][111],G[111][111];
void floyd()
{
int i,j,k;
for(i=1;i<=n+1;i++)
{
for(j=1;j<=n+1;j++)
{
dis[i][j]=G[i][j];
path[i][j]=-1;
}
}
for(k=1;k<=n+1;k++)
{
for(i=1;i<=n+1;i++)
{
for(j=1;j<=n+1;j++)
{
if(dis[i][j]>dis[i][k]+dis[k][j])
{
dis[i][j]=dis[i][k]+dis[k][j];
path[i][j]=k;
} }
}
}
}
void dfs(int i,int j)//中序遍历输出路径
{
int k;
k=path[i][j];
if(k==-1)
return ; dfs(i,k);
printf("%d->",k);
dfs(k,j); }
int main()
{
int i,T,a[111],m,j,kk=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
a[n+1]=a[1];
scanf("%d",&m);
for(i=1;i<=n+1;i++)
{
for(j=1;j<=n+1;j++)
{
G[i][j]=inf;
}
G[i][i]=0;
}
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
G[u][v]=-a[v];
} floyd();
if(kk!=1)
printf("\n");
printf("CASE %d#\n",kk++);
printf("points : %d\n",-dis[1][1+n]);
printf("circuit : 1->");
dfs(1,1+n);
printf("1\n"); }
return 0;
}
Floyd算法并输出路径的更多相关文章
- Floyd算法——保存路径——输出路径 HDU1385
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1385 参考 http://blog.csdn.net/shuangde800/article/deta ...
- SPFA和FLOYD算法如何打印路径
早晨碰到了一题挺裸的最短路问题需要打印路径:vijos1635 1.首先说说spfa的方法: 其实自己之前打的最多的spfa是在网格上的那种,也就是二维的 一维的需要邻接表+queue 以及对于que ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- Floyd最短路(带路径输出)
摘要(以下内容来自百度) Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似. 该算法名称以创始人之一.1978年图灵奖获得者. ...
- URAL 1004 Sightseeing Trip(floyd求最小环+路径输出)
https://vjudge.net/problem/URAL-1004 题意:求路径最小的环(至少三个点),并且输出路径. 思路: 一开始INF开大了...无限wa,原来相加时会爆int... 路径 ...
- [Python] 弗洛伊德(Floyd)算法求图的直径并记录路径
相关概念 对于一个图G=(V, E),求图中两点u, v间最短路径长度,称为图的最短路径问题.最短路径中最长的称为图的直径. 其中,求图中确定的某两点的最短路径算法,称为单源最短路径算法.求图中任意两 ...
- Codefroces Gym101572 I.Import Spaghetti-有向图跑最小环输出路径(Floyd)
暑假学的很多东西,现在都忘了,补这道题还要重新学一下floyd,有点难过,我暑假学的东西呢??? 好了,淡定,开始写题解. 这个题我是真的很难过啊,输入简直是有毒啊(内心已经画圈诅咒出题人无数次了.. ...
- 最小路径算法(Dijkstra算法和Floyd算法)
1.单源点的最短路径问题:给定带权有向图G和源点v,求从v到G中其余各顶点的最短路径. 我们用一个例子来具体说明迪杰斯特拉算法的流程. 定义源点为 0,dist[i]为源点 0 到顶点 i 的最短路径 ...
- HD1385Minimum Transport Cost(Floyd + 输出路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
随机推荐
- bt开源的客户端——xbt client
我部署好了bt tracker, 用bitcomet可以下载. 但xbt client下载不来.torrent资源.
- Oracle 12c安装详细步骤,带截图
1,在官网上下载oracle的压缩文件,两个都要下载. 并两个同时选中解压在一个文件夹里面. 2,解压之后,如下图,点击setup.exe稍等一会儿 ,3,开始安装: 不选点击下一步,或者直接点击下一 ...
- Office密码破解不求人!
你用Office吗?你会为你的Office文档加密吗?如果Office密码忘了求人吗?最后一个问题是不是让你很头大,求人办事不是要费钱就是要靠人情,不如自己拥有一款强大的密码破解工具,想要Office ...
- linux中,如何设置每隔2个小时就执行一次某个脚本?
需求描述: 今天同事问了一个linux上crontab定时任务的问题,说,如何调整一个定时任务每2个小时 执行一次,在此记录下. 操作过程: 1.通过以下的方式设置,每2个小时执行一次脚本 */ * ...
- ubuntu网页无法看视频
sudo apt-get install flashplugin-nonfree sudo apt-get install aptitude sudo aptitude install ubuntu- ...
- JavaScript------字符串与HTML格式相互转换
转载: http://blog.sina.com.cn/s/blog_4cb0b0fc0100aoo1.html 代码:: 1.将字符转换成Html function encodeHtml(str){ ...
- Python学习笔记(五)OOP
模块 使用模块import 模块名.有的仅仅导入了某个模块的一个类或者函数,使用from 模块名 import 函数或类名实现.为了避免模块名冲突.Python引入了按文件夹来组织模块的方法,称为包( ...
- 工作流JBPM_day01:7-使用流程变量
工作流JBPM_day01:7-使用流程变量 工作流就像流水线 对应数据库中的一张表 ProcessVariableTest.Java import java.util.List; import or ...
- JBPM4.4_核心概念与相关API
1. 核心概念与相关API(Service API) 1.1. 概念:Process definition, process instance , execution 1.1.1. Process ...
- 超全面的JavaWeb笔记day10<Response&Request&路径&编码>
1.Response 2.Request 3.路径 4.编码 请求响应流程图 response 1.response概述 response是Servlet.service方法的一个参数,类型为java ...