POJ 3615 floyd 求任意起点终点的最短路
生活随笔
收集整理的這篇文章主要介紹了
POJ 3615 floyd 求任意起点终点的最短路
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 求任意起点终点的最短路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 光标移动到输入框最后位置函数
- 下一篇: eclipse中使用javap分析jav