hdu1224

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

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 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?
 
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(极光炫影)
 

#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算法并输出路径的更多相关文章

  1. Floyd算法——保存路径——输出路径 HDU1385

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1385 参考 http://blog.csdn.net/shuangde800/article/deta ...

  2. SPFA和FLOYD算法如何打印路径

    早晨碰到了一题挺裸的最短路问题需要打印路径:vijos1635 1.首先说说spfa的方法: 其实自己之前打的最多的spfa是在网格上的那种,也就是二维的 一维的需要邻接表+queue 以及对于que ...

  3. ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)

    题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...

  4. Floyd最短路(带路径输出)

    摘要(以下内容来自百度) Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似. 该算法名称以创始人之一.1978年图灵奖获得者. ...

  5. URAL 1004 Sightseeing Trip(floyd求最小环+路径输出)

    https://vjudge.net/problem/URAL-1004 题意:求路径最小的环(至少三个点),并且输出路径. 思路: 一开始INF开大了...无限wa,原来相加时会爆int... 路径 ...

  6. [Python] 弗洛伊德(Floyd)算法求图的直径并记录路径

    相关概念 对于一个图G=(V, E),求图中两点u, v间最短路径长度,称为图的最短路径问题.最短路径中最长的称为图的直径. 其中,求图中确定的某两点的最短路径算法,称为单源最短路径算法.求图中任意两 ...

  7. Codefroces Gym101572 I.Import Spaghetti-有向图跑最小环输出路径(Floyd)

    暑假学的很多东西,现在都忘了,补这道题还要重新学一下floyd,有点难过,我暑假学的东西呢??? 好了,淡定,开始写题解. 这个题我是真的很难过啊,输入简直是有毒啊(内心已经画圈诅咒出题人无数次了.. ...

  8. 最小路径算法(Dijkstra算法和Floyd算法)

    1.单源点的最短路径问题:给定带权有向图G和源点v,求从v到G中其余各顶点的最短路径. 我们用一个例子来具体说明迪杰斯特拉算法的流程. 定义源点为 0,dist[i]为源点 0 到顶点 i 的最短路径 ...

  9. HD1385Minimum Transport Cost(Floyd + 输出路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

随机推荐

  1. Hadoop2的Yarn和MapReduce2相关

    转自: http://www.aboutyun.com/thread-7678-1-1.html..   问题导读: 1.什么是yarn? 2.Yarn 和MapReduce相比,它有什么特殊作用 ? ...

  2. php 删除目录

    <?php /* 自定义的删除函数,可以删除文件和递归删除文件夹 */ function my_del($path)//自定义my_del函数,函数有一个参数($path).当调用my_del( ...

  3. Incentivizing exploration in reinforcement learning with deep predictive models

    Stadie, Bradly C., Sergey Levine, and Pieter Abbeel. "Incentivizing exploration in reinforcemen ...

  4. i2c驱动笔记

    基于bcm5300x芯片 注册平台总线设备,设备名bcm5300x_i2c,通过名称与驱动进行匹配. 注册平台总线驱动.驱动名称"bcm5300x_i2c",与设备进行匹配. dr ...

  5. erlang的简单模拟半包的产生

     gen_tcp:linsten()/2使用的是{packet,2/4/8},则gen_tcp模块在接受或者发送时自动除去包头或者自动加上包头. 本例中使用的是{packet,0}. -module( ...

  6. razor使用注意点........

    使用三元运算符时记得加括号.... 如: @Convert.ToInt32(Request.QueryString["type"])==0?true:false :这是错误的写法 ...

  7. c++ 参赛设置

    void report(LogWriter& lw); 代表引用原对象 void report(LogWriter lw); 代表重新拷贝构造一个对象

  8. python2.0_day18_django_ORM

    Django_ORMday15\16我们学到的ORM都是最基本的增删改查.下面我们稍作升级,学习下高级点的增删改查.先创建一个新的APP项目 python3. manage.py startapp b ...

  9. Dubbo(二) -- Simple Monitor

    一.简介 dubbo-monitor-simple是dubbo提供的简单监控中心,可以用来显示接口暴露,注册情况,也可以看接口的调用明细,调用时间等. Simple Monitor挂掉不会影响到Con ...

  10. Linux wc 命令

    wc命令可以用来统计文件的行数 .单词数 .字符数,用法如下: [root@localhost ~]$ wc 1.txt # 统计文件的行数.单词数.字符数 2 4 24 1.txt [root@lo ...