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 ...
随机推荐
- CI循环数组问题
当我们在Controll中把数据传递到view中如: $data['cates_data']=$this->Category_Model->byid_data($id); #调用模型层查询 ...
- ElasticSearch0910学习
1:es简介 es是一个分布式的搜索引擎,使用java开发,底层使用lucene. 特点:天生支持分布式的.为大数据而生的.基于restful接口. 2:es和solr对比 接口 solr:类似web ...
- 关于用Cocos2d-x.3.10运行别人游戏项目的步骤
1.首先打开Cocos那个一体化软件. 2.创建工程,取名字,选择路径. 3.用VS2013打开新建的项目. 4.打开cocosdata(我自己放游戏项目的文件目录)的相应项目(自己刚创建的). 5. ...
- hdu 1281 棋盘游戏 (二分匹配)
//是象棋里的车 符合二分匹配 # include<stdio.h> # include<algorithm> # include<string.h> using ...
- c++ json cpp
一 编译链接 1 在相应官网下载jsoncpp 2 解压得到jsoncpp-src-0.5.0文件 3 打开jsoncpp-src-0.5.0 -> makefiles -> vs71 - ...
- WPF: 把引用的dll移动到自定义路径
需求: 有A.exe和B.exe, 都引用了 C.dll, output路径都是 W:\Debug. A和B都添加了对C的引用,正常情况下C会被复制到 output 里面. C这样子的dll很多,不想 ...
- PHPCMS v9在后台文章管理列表添加类别
进入PHPCMS v9后台—内容,进入PHPCMS的文章管理列表,要实现在文章标题前显示文章类别,就是可以直接在文章列表里看到类别,不需要点击进入编辑页面才可以看到,如下图: PHPCMS v9在后台 ...
- mysql数据库,如何进行数据目录的初始化操作
需求描述: 当将mysql的二进制包解压之后,只有通过初始化数据库的数据目录,创建系统表之后才能使用mysql Server 数据库版本:5.5.57-log 操作过程: 1.通过解压之后的mysql ...
- wsdl.exe的用法与参数说明
wsdl.exe的用法与参数说明 打开.net自己带的Visual Studio .NET 2003或 2005 命令提示 输入 wsdl /language:VB /n:mynamespace /o ...
- ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...