首先贴一篇文章 讲的特别好最短路的三种算法(Floyd、Dijkstra、SPFA)


A-最短路

在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

Input

输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。

Output

对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间

Sample Input

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

Sample Output

3
2

題解:
Dijkstra的模板題

#include<iostream>
using namespace std;
int map[105][105],dis[105],vis[105];
int n,m,u,v,t;
#define INF 0x3f3f3f3fvoid Dijkstra(int src){for(int i=1;i<=n;i++){//初始化所有点到起点的最小距离 dis[i]=map[src][i];vis[i]=0;}vis[src]=1;for(int i=1;i<=n;i++){int ans=INF,k;for(int j=1;j<=n;j++){if(!vis[j]&&dis[j]<ans){//寻找未被访问过但是离起点最近的点 k=j;ans=dis[j];}}vis[k]=1;//标记已经被访问过if(ans==INF)break;//剩下的点都不通//下面的是松弛操作 for(int j=1;j<=n;j++){if(!vis[j]&&dis[k]+map[j][k]<dis[j]){dis[j]=map[j][k]+dis[k];}} }printf("%d\n",dis[n]);
}int main(){while(~scanf("%d %d",&n,&m)){if(n==0&&m==0)break;//下面是初始化 for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i==j)map[i][j]=0;//自己到自己的距离else map[i][j]=INF;//初始化到无穷大 }}for(int i=1;i<=m;i++){scanf("%d %d %d",&u,&v,&t);if(t<map[u][v])map[u][v]=map[v][u]=t;//更新保持map数组储存两地最小距离 }Dijkstra(1);}return 0;
}

B題和C題都是Dijkstra的模板題,就略了。
就是要仔細看樣例的數據。C題看錯數據,還以爲自己做錯了,手打了3遍代碼,難受

要注意的是,要分清方向是單向還是雙向


D - Heavy Transportation

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

大致題意:
有T組測試數據,對於每組數據,有n個地點,和m條道路。每條道路都有最大載重量。求出從1到n的最大載重量。

題解:
也可以用Dijkstra的模板來做,但是要把關係改成max和min

格式問題也要注意一下

#include<cstring>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int map[1010][1010],dis[1010];
bool vis[1010];
int n,m,T,u,v,t;
void Dijkstra(){for(int i=1;i<=n;i++){dis[i]=map[1][i];vis[i]=0;}for(int i=1;i<=n;i++){int max=-INF,x;for(int j=1; j<=n; j++)if(!vis[j]&&dis[j]>max) {max=dis[j];x=j;}vis[x]=1;for(int j=1; j<=n; j++)if(!vis[j]&&dis[j]<min(dis[x],map[x][j]))dis[j]=min(dis[x],map[x][j]);}
}
int main(){scanf("%d",&T);for(int cnt=1;cnt<=T;cnt++){scanf("%d %d",&n,&m);for(int i=1;i<=n; i++)for(int j=1;j<=n;j++){if(i==j)map[i][j]=0;else map[i][j]=-INF;}for(int i=1;i<=m;i++){scanf("%d %d %d",&u,&v,&t);if(map[u][v]<t)map[u][v]=map[v][u]=t;}Dijkstra();printf("Scenario #%d:\n%d\n\n",cnt,dis[n]);}
}

E - Cow Contest

N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input

  • Line 1: Two space-separated integers: N and M
  • Lines 2…M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output

  • Line 1: A single integer representing the number of cows whose ranks can be determined

Sample Input

5 5
4 3
4 2
3 2
1 2
2 5

Sample Output

2

大致題意:
對於每組數據,有n頭奶牛和m個勝負關係。接下來2-m+1行,輸入a打敗了b的關係。輸出可以得出排名關係的奶牛數量

題解:
首先肯定是要輸入給出的關係的,然後用Floyd函數更新一下可以傳遞得出的關係,那麽有n-1個關係(除了和自己,知道有沒有戰勝全部別的奶牛)就是題目要求的奶牛,算出數量即可

#include<cstring>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int map[1010][1010],dis[1010];
bool vis[1010];
int n,m,T,u,v,t;
void Dijkstra(){for(int i=1;i<=n;i++){dis[i]=map[1][i];vis[i]=0;}for(int i=1;i<=n;i++){int max=-INF,x;for(int j=1; j<=n; j++)if(!vis[j]&&dis[j]>max) {max=dis[j];x=j;}vis[x]=1;for(int j=1; j<=n; j++)if(!vis[j]&&dis[j]<min(dis[x],map[x][j]))dis[j]=min(dis[x],map[x][j]);}
}
int main(){scanf("%d",&T);for(int cnt=1;cnt<=T;cnt++){scanf("%d %d",&n,&m);for(int i=1;i<=n; i++)for(int j=1;j<=n;j++){if(i==j)map[i][j]=0;else map[i][j]=-INF;}for(int i=1;i<=m;i++){scanf("%d %d %d",&u,&v,&t);if(map[u][v]<t)map[u][v]=map[v][u]=t;}Dijkstra();printf("Scenario #%d:\n%d\n\n",cnt,dis[n]);}
}

2021-1-30最短路入门相关推荐

  1. Revit结构2021专业人士的选择:从入门到专业

    Revit结构2021专业人士的选择:从入门到专业 Revit Structure 2021 : 13th Floor Concrete Building 你会学到: Revit结构中的第13栋混凝土 ...

  2. 2016windows(10) wamp 最简单30分钟thrift入门使用讲解,实现php作为服务器和客户端的hello world...

    2016最简单windows(10) wamp 30分钟thrift入门使用讲解,实现php作为服务器和客户端的hello world thrift是什么 最简单解释 thrift是用来帮助各个编程语 ...

  3. 【30天从入门到放弃】我的机器学习之路 4

    周末注册了kaggle,为了先熟悉一下这个平台.今天用了大半天的时间刷完了一个新手副本任务--泰坦尼克号存活率预测(这个应该算是kaggle上的"hello world"级别的项目 ...

  4. 【2021】网络协议从入门到底层原理-MJ【新】附上下载链接

    [2021]网络协议从入门到底层原理-MJ大神[新]附上下载链接 掌握熟悉的HTTP协议以外,还要学习更多网络协议的相关知识,比如底层的TCP\UDP协议.流媒体的RTMP协议等. 适合人群 1.本课 ...

  5. 2021数学基础30讲扫描版 网盘(里面直接是文档,免费)

    2021数学基础30讲扫描版 网盘(里面直接是文档,无需付费) 考研数学基础(数一二三通用) https://pan.baidu.com/s/1tq_GzdapBpiPxjtu7entEw 提取码:s ...

  6. 2021/10/30的1+X大数据Java答案

    2021/10/30 步骤二 public Member() { }public Member(String name,String pwd,float score,int rank) {this.n ...

  7. 2021.1.30课程摘要(逻辑教育-王劲胜)

    2021.1.30课程摘要 逻辑教育-13期-Python基础班-王劲胜 一.集合 二.函数(上) 三.作业讲解 逻辑教育-13期-Python基础班-王劲胜 一.集合 1.集合简介 • 集合表现形式 ...

  8. 2021.03.30【2021省赛】模拟 比赛总结

    2021.03.30[2021省赛]模拟 比赛总结 地址: https://gmoj.net/senior/#contest/home/3350 T1: 神奇纸牌(uno) T2: 凌乱平衡树 (tr ...

  9. 全网最详细中英文ChatGPT接口文档(六)30分钟快速入门ChatGPT——使用策略和API数据使用策略

    30分钟快速入门使用ChatGPT--使用策略和API数据使用策略 Usage policies使用策略 Disallowed usage of our models 禁止使用我们的模型 API da ...

  10. JZOJ 7036. 2021.03.30【2021省赛模拟】凌乱平衡树(平衡树单旋+权值线段树)

    JZOJ 7036. 2021.03.30[2021省赛模拟]凌乱平衡树 题目大意 给出两棵Treap,大小分别为 n , m n,m n,m,每个点的 p r i o r i t y priorit ...

最新文章

  1. Google protobuf使用技巧和经验
  2. svn使用经验---不断总结
  3. MySQL慢查询日志总结
  4. php 头bom_关于php中bom头的简介
  5. POJ - 2594 Treasure Exploration(最小路径覆盖-二分图最大匹配+传递闭包)
  6. powerbi导入地图_Power BI系列教程之powerBI功能介绍及使用导引(一)
  7. 账户Account类文件编写(static成员使用)
  8. c语言加速度积分得到速度_自编微积分教材-第一章 微积分漫谈(1)
  9. (第一周)软件工程四人组
  10. ajax提交表单序列化(serialize())数据
  11. 系统设计与任务分配(团队作业)
  12. Oracle 10g Create Database
  13. (pytorch)yolov3训练自己的模型
  14. Linux多线程编程之pthread
  15. 友善串口工具接收数据随机换行_mfc串口收发数据。编辑框希望每接收一次就换行。怎么实现。我的换行结果不对...
  16. Python 实战案例--计算圆、矩形的周长和面积
  17. 解决Mygui不支持中文路径
  18. 串行同步通信“时钟同步”怎么理解?
  19. Linux读取串口数据
  20. 盘点Galaxy S6 edge+几个趣味功能

热门文章

  1. Linux系统下KVM虚拟机的基本管理和操作
  2. linux常用命令100
  3. LTE CQI/PMI 上报机制
  4. SHELL编程基础 By jackie
  5. 淘宝美工图片处理规范
  6. 索尼爱立信滑盖机java_可爱Walkman滑盖机 索尼爱立信W100i评测
  7. 相控阵天线(三):直线阵列天线低副瓣综合(切比雪夫、泰勒分布、SinZ-Z和Villeneuve分布、含python代码)
  8. python-文件读写-OS-窗口控制
  9. NLP系列(8)_用可视化解构BERT,从上亿参数中提取出的6种直观模式
  10. day 32 子进程的开启 及其用法