http://poj.org/problem?id=3615

题意:求起点到终点的最短路,不存在则输出-1.这题居然tle两次,把floyd放在外面就行了。

View Code

// I'm lanjiangzhou
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif//**************************CONSTANT***********************
#define INF 0x3f3f3f3f
#define eps 1e-8
#define PI acos(-1.)
#define PI2 asin (1.);
typedef long long LL;
//typedef __int64 LL;   //codeforces
typedef unsigned int ui;
typedef unsigned long long ui64;
#define MP make_pair
typedef vector<int> VI;
typedef pair<int, int> PII;
#define pb push_back
#define mp make_pair//***************************SENTENCE************************
#define CL(a,b) memset (a, b, sizeof (a))
#define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b))
#define sqr3(a,b,c) sqrt((double)(a)*(a) + (double)(b)*(b) + (double)(c)*(c))//****************************FUNCTION************************
template <typename T> double DIS(T va, T vb) { return sqr(va.x - vb.x, va.y - vb.y); }
template <class T> inline T INTEGER_LEN(T v) { int len = 1; while (v /= 10) ++len; return len; }
template <typename T> inline T square(T va, T vb) { return va * va + vb * vb; }// aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//endconst int maxn =1010;
int edges[maxn][maxn];
int n,m,t;
int u,v,w;
int maxx(int a,int b){return a>b?a:b;
}
int minn(int a,int b){return a>b?b:a;
}void floyd(){for(int k=1;k<=n;k++){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){edges[i][j]=minn(edges[i][j],maxx(edges[i][k],edges[k][j]));}}}
}int main(){while(scanf("%d%d%d",&n,&m,&t)!=EOF){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){edges[i][j]=INF;}edges[i][i]=0;}for(int i=1;i<=m;i++){scanf("%d%d%d",&u,&v,&w);edges[u][v]=w;}int start,end;floyd();//放在外面,里面超时for(int i=1;i<=t;i++){scanf("%d%d",&start,&end);if(edges[start][end]==INF){printf("-1\n");}else printf("%d\n",edges[start][end]);}}//floyd();return 0;
}

转载于:https://www.cnblogs.com/lanjiangzhou/archive/2013/04/02/2995962.html

POJ 3615 floyd 求任意起点终点的最短路相关推荐

  1. The Geodetic Set Problem - POJ 1612 Floyd求最短路径所有点集

    题目链接 POJ 1612 Description Let G = (V,E) be a connected graph without loops and multiple edges, where ...

  2. Poj(2240),Floyd求汇率是不是赚钱

    题目链接:http://poj.org/problem?id=2240. Floyd算法修改一下,我要最大路径(通过转汇率变到最大)改成max. #include <iostream> # ...

  3. poj 3660(Floyd求传递闭包)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9317   Accepted: 5249 Descr ...

  4. matlab求任意点最短路径,【最短路】求两点间最短路径的改进的Dijkstra算法及其matlab实现...

    代码来源:<图论算法及其matlab实现>(北京航空航天出版社) P18 书中提出了基于经典Dijkstra算法改进的两种算法. 其中算法Ⅱ的效率较高. 代码如下: 1 function ...

  5. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  6. 用Postgis算最短路径(在任意位置选择起点终点)

    1.前言 阅读本文需要知道什么是shapfile,什么是路径分析,什么是GIS.相比Arcgis的路径分析功能,本文介绍的方法稍微复杂,需要注意的细节更多,但却是完全免费的.PostGis+QGIS+ ...

  7. java gis 最短路径_用Postgis算最短路径(在任意位置选择起点终点)(下)

    今天将上一次路径绘制的坑(上次绘制的是子路径端点的连线)给填了,本文中我将实现一个输入起点和终点经纬度,输出完整路径(包含子路径端点间的真实弯曲情况,完美贴合真实路径)的函数,能够计算准确的路径长度. ...

  8. floyd求最小环 模板

    http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...

  9. ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760 题意: 给出一张有向带边权图,若不联通为-1.问不重叠的最 ...

最新文章

  1. git学习笔记(四)—— 分支管理
  2. win10下使用wget
  3. 【CyberSecurityLearning 36】靶场环境搭建(ubuntu系统安装优化及vulhub安装)
  4. SharePoint 2010 产品六大功能模块
  5. C# Windows服务
  6. 2020第十一届蓝桥杯软件类省赛第二场C/C++ 大学 B 组(题解)
  7. 语音识别学习日志 2019-7-14 语音识别基础知识准备3 {Kmean算法分析与HMM(Hidden Markov Model)模型}
  8. 麦肯锡在全球调研分析160个案例,给出5个行业的34个AI应用场景
  9. 路由信息协议工作原理
  10. Bailian3728 Blah数集【数学+set】
  11. 数组元素替换_LeetCode基础算法题第183篇:一维数组的重新洗牌
  12. Python自动化运维_批量执行py脚本
  13. 张开涛 shiro 博客地址
  14. 如何安装perl模块
  15. vue通过disabled控制按钮的置灰
  16. 360极速模式版本,出现已经取消网页导航现象,怎么解决呢?
  17. Spreadsheet_Excel_Writer
  18. 空间尺寸对迭代次数的影响
  19. Hadoop数据仓库hive的应用
  20. 淘宝运营 钻展基本了解 钻展投放方案 钻展投放注意的点

热门文章

  1. POJ 1185 炮兵阵地 【状压DP】
  2. Windows 10 UWP开发:如何不让界面卡死
  3. 主攻ASP.NET.4.5.1 MVC5.0之重生:Web项目语音朗读网页文本,简单语音提示浏览状态
  4. [转]sqlplus中不能上下键选择前一条命令解决方法
  5. 兔子不吃窝边草,跳槽不跳窝边槽。。。
  6. [日常] 算法-旋转字符串-暴力移位法
  7. 如何利用阿里云安全产品加强你的网站防护能力
  8. Xcode多Target设置
  9. Android 多媒体------相机
  10. hiho_1089_floyd最短路