有四种类型;

单源:dij,spfa,bellman-ford

多源:floyd

dij有两种:

一个复杂度为n^2,一个复杂度是m*logn

畅通工程续

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

Input本题目包含多组数据,请处理到文件结束。

每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。

接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。

再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。Output对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

Sample Input

3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

2
-1
#include <stdio.h>//n^2
#include <stdlib.h>
#include <string.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=220;
int w[maxn][maxn],d[maxn];
bool vis[maxn];
int n,m; void dijkstra(int s)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++) d[i]=inf;
d[s]=0; for(int i=0;i<n;i++)
{
int x=-1,y=inf;
for(int j=0;j<n;j++)
{
if(vis[j]==0&&d[j]<=y) y=d[x=j];
}
if(x==-1) continue;
vis[x]=1;
for(int j=0;j<n;j++)
{
if(!vis[j]&&d[j]>d[x]+w[x][j]) d[j]=d[x]+w[x][j];
}
}
} int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==j) w[i][j]=0;
else w[i][j]=inf;
}
}
for(int i=0;i<m;i++)
{
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
if(w[a][b]>x) w[a][b]=w[b][a]=x;
}
int s,t;
scanf("%d%d",&s,&t);
dijkstra(s);
if(d[t]>=inf) printf("-1\n");
else printf("%d\n",d[t]);
}
return 0;
}

  

#include <stdio.h>//mlogn
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1100;
int d[maxn],n,m;
bool vis[maxn];
struct edge
{
int from,to,dist;
edge(int from,int to,int dist):from(from),to(to),dist(dist){}
};
struct heapnode
{
int d,u;
heapnode(int d,int u) : d(d),u(u) {}
bool operator<(const heapnode &a) const{
return a.d<d;
}
}; queue<edge>q[maxn]; void dijkstra(int s)
{
priority_queue<heapnode>que;
for(int i=0;i<=n;i++) d[i]=inf;
d[s]=0;
memset(vis,0,sizeof(vis));
que.push(heapnode(0,s));
while(!que.empty())
{
heapnode x=que.top();que.pop();
int u=x.u;
if(vis[u]) continue;
vis[u]=1;
while(!q[u].empty())
{
edge e=q[u].front();
q[u].pop();
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
que.push(heapnode(d[e.to],e.to));
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++) while(!q[i].empty()) q[i].pop();
for(int i=0;i<m;i++)
{
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
q[a].push(edge(a,b,x));
q[b].push(edge(b,a,x));
}
int s,t;
scanf("%d%d",&s,&t);
dijkstra(s);
if(d[t]>=inf) printf("-1\n");
else printf("%d\n",d[t]);
}
return 0;
}

  

#include <stdio.h>//mlogn
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1100;
int d[maxn],n,m;
bool vis[maxn];
struct edge
{
int from,to,dist;
edge(int from,int to,int dist):from(from),to(to),dist(dist){}
};
struct heapnode
{
int d,u;
heapnode(int d,int u) : d(d),u(u) {}
bool operator<(const heapnode &a) const{
return a.d<d;
}
}; vector<edge> vec;
vector<int> g[maxn]; void dijkstra(int s)
{
priority_queue<heapnode>que;
for(int i=0;i<=n;i++) d[i]=inf;
d[s]=0;
memset(vis,0,sizeof(vis));
que.push(heapnode(0,s));
while(!que.empty())
{
heapnode x=que.top();que.pop();
int u=x.u;
if(vis[u]) continue;
vis[u]=1;
for(int i=0;i<g[u].size();i++)
{
edge &e=vec[g[u][i]];
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
que.push(heapnode(d[e.to],e.to));
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<=n;i++) g[i].clear();
vec.clear();
for(int i=0;i<m;i++)
{
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
vec.push_back(edge(a,b,x));
int c=vec.size();
g[a].push_back(c-1);
vec.push_back(edge(b,a,x));
c=vec.size();
g[b].push_back(c-1);
}
int s,t;
scanf("%d%d",&s,&t);
dijkstra(s);
if(d[t]>=inf) printf("-1\n");
else printf("%d\n",d[t]);
}
return 0;
}

  

C - find the longest of the shortest

Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.

Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.

InputEach case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1, and Marica in city N.

In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.OutputIn the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.Sample Input

5 6
1 2 4
1 3 3
2 3 1
2 4 4
2 5 7
4 5 1 6 7
1 2 1
2 3 4
3 4 4
4 6 4
1 5 5
2 5 2
5 6 5 5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10

Sample Output

11
13
27 这个和之前差不多,有一点点区别,就是让你求出最短路,然后在最短路里面枚举每一边断了,这个最坏的情况用的时间。 这个用邻接矩阵写的,用队列的太麻烦
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1100;
int w[maxn][maxn];
int d[maxn];
int path[maxn];
bool flag,vis[maxn];
int n,m; void dijkstra(int s)
{
for(int i=0;i<=n;i++)
{
d[i]=inf;
vis[i]=0;
}
d[s]=0; for(int i=1;i<=n;i++)
{
int x,m=inf;
for(int j=1;j<=n;j++) if(!vis[j]&&d[j]<m) m=d[x=j];
vis[x]=1;
for(int j=1;j<=n;j++)
{
if(d[j]>d[x]+w[x][j])
{
d[j]=d[x]+w[x][j];
if(!flag) path[j]=x;
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) w[i][j]=0;
else w[i][j]=inf;
}
}
for(int i=1;i<=m;i++)
{
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
w[a][b]=x;
w[b][a]=x;
}
flag=0;
dijkstra(1);
// printf("%d\n",d[n]);
flag=1;
int tmp,ans=d[n];
for(int i=n;i>1;i=path[i])
{
tmp=w[path[i]][i];
w[path[i]][i]=inf;
dijkstra(1);
w[path[i]][i]=tmp;
if(d[n]>=inf) continue;
ans=max(ans,d[n]);
// printf("%d\n",i);
}
printf("%d\n",ans);
}
return 0;
}

  

D - Frogger

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.

To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.

The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.

Input

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

Sample Input

2
0 0
3 4 3
17 4
19 4
18 5 0

Sample Output

Scenario #1
Frog Distance = 5.000 Scenario #2
Frog Distance = 1.414 这个题目有点特别,要结合贪心,在d[i]的i的取值的时候要注意不是直接取求出到达i的最短路径长度,而是,从1号位置到这个位置的最长边的最小值。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=250;
int n;
double d[maxn];
bool vis[maxn];
double w[maxn][maxn]; struct node
{
double x,y;
node(double x=0,double y=0):x(x),y(y){}
}exa[maxn]; void dijkstra()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) d[i]=w[1][i];
for(int i=1;i<=n;i++)
{
int x=-1;
double m=inf;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&d[j]<m) m=d[x=j];
}
if(x!=-1)
{
vis[x]=1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&d[j]>max(d[x],w[x][j])) d[j]=max(d[x],w[x][j]);
}
}
}
} int main()
{
int cnt=0;
while(scanf("%d\n",&n)!=EOF&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&exa[i].x,&exa[i].y);
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
w[i][j]=w[j][i]=sqrt((exa[i].x-exa[j].x)*(exa[i].x-exa[j].x)+(exa[i].y-exa[j].y)*(exa[i].y-exa[j].y));
}
}
dijkstra();
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",++cnt,d[2]);
}
return 0;
}

  

最短路 summary的更多相关文章

  1. Summary of Critical and Exploitable iOS Vulnerabilities in 2016

    Summary of Critical and Exploitable iOS Vulnerabilities in 2016 Author:Min (Spark) Zheng, Cererdlong ...

  2. 三个不常用的HTML元素:<details>、<summary>、<dialog>

    前面的话 HTML5不仅新增了语义型区块级元素及表单类元素,也新增了一些其他的功能性元素,这些元素由于浏览器支持等各种原因,并没有被广泛使用 文档描述 <details>主要用于描述文档或 ...

  3. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  4. [LeetCode] Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  5. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  6. Network Basic Commands Summary

    Network Basic Commands Summary set or modify hostname a)     temporary ways hostname NEW_HOSTNAME, b ...

  7. Summary - SNMP Tutorial

    30.13 Summary Network management protocols allow a manager to monitor and control routers and hosts. ...

  8. Mac Brew Install Nginx Summary

    ==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.1.el_capitan.bot################# ...

  9. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

随机推荐

  1. .NET Core 实践二:事件通知和异步处理

    首先让我们来先看一个例子: 这是一个简单的用户下单购买商品的业务模型,输入端是用户,相关物料有订单和货物,相关的内部服务有业务(订单).财务(支付).仓储(备货)和物流(运输). 从图中我们可以看到, ...

  2. JQuery官方学习资料(译):$( document ).ready()

         一个页面直到document是”ready“才能被安全的操作,Jquery为你检查这种状态.代码包含在$( document ).ready()的内部将会仅仅运行一次在页面Document ...

  3. java反射知识相关的文章

    整理的反射相关的文章: (1).通俗理解反射(知乎):学习java应该如何理解反射? (2).关于反射比较深入的博文地址:深入解析Java反射(1) - 基础 贴出我反射调用代码:(craw,dept ...

  4. python中的property属性

    目录 1. 什么是property属性 2. 简单的实例 3. property属性的有两种方式 3.1 装饰器方式 3.2 类属性方式,创建值为property对象的类属性 4. property属 ...

  5. 我写的.net相关的文章

    此文正在更新中... 广州.net俱乐部相关 复活广州.net俱乐部 office365的开发者训练营,免费,在微软广州举办 被低估的.net(上) - 微软MonkeyFest 2018广州分享会活 ...

  6. 快速开发跨平台应用之Xamarin技术

    Xamarin 介绍 Xamarin 是一个允许开发人员有效创建可跨 iOS.Android.Windows 应用程序的开发工具集.Xamarin是免费且开源的,遵循 MIT (麻省理工学院许可证)协 ...

  7. 深圳市共创力推出《以用户为中心的设计UCD方法与实战》课程!

    以用户为中心的设计(UCD)方法与实战 课程特色 现在以市场为中心.科技为基础.体验为卖点的商业社会里,用户体验是赢得用户青睐的关键特性.苹果.google.腾讯等顶级企业的成功充分说明了这一点.如何 ...

  8. 原型模式ProtoType

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/3/4 21:49 # @Author : ChenAdong # @emai ...

  9. spring4笔记----PropertyOverrideConfigureer 重写占位符配置器(图)

  10. sql 语句-初级进阶(一)

    以下所有的sql语句是根据个人资料进行操作,为方便大家操作联系,附上链接:: 链接:https://pan.baidu.com/s/14LmWyhJPQRzpjURQBKM4mA 提取码:wu1q  ...