图论trainning-part-1 A. 最短路
A. 最短路
64-bit integer IO format: %I64d Java class name: Main
Input
输入保证至少存在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
- 解题:最短路。。。
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <vector>
- #include <queue>
- #include <cstdlib>
- #include <string>
- #include <set>
- #define LL long long
- #define INF 0x3f3f3f
- using namespace std;
- int mp[][],d[],n,m;
- bool vis[];
- void dij(){
- int i,j,theMin,index;
- for(i = ; i <= n; i++)
- d[i] = INF>>;
- d[] = ;
- memset(vis,false,sizeof(vis));
- for(i = ; i < n; i++){
- theMin = INF;
- for(j = ; j <= n; j++){
- if(!vis[j] && theMin > d[j]) theMin = d[index = j];
- }
- vis[index] = true;
- if(index == n) break;
- for(j = ; j <= n; j++){
- if(!vis[j] && d[j] > d[index]+mp[index][j])
- d[j] = d[index] + mp[index][j];
- }
- }
- }
- int main(){
- int i,j,u,v,w;
- while(scanf("%d%d",&n,&m),n||m){
- for(i = ; i <= n; i++)
- for(j = ; j <= n; j++)
- mp[i][j] = INF;
- for(i = ; i < m; i++){
- scanf("%d%d%d",&u,&v,&w);
- mp[u][v] = mp[v][u] = w;
- }
- dij();
- printf("%d\n",d[n]);
- }
- return ;
- }
优先队列版Dijkstra
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <vector>
- #include <queue>
- #include <climits>
- #include <algorithm>
- #include <cmath>
- #define LL long long
- #define INF 0x3f3f3f
- using namespace std;
- #define pii pair<int,int>
- const int maxn = ;
- struct arc {
- int to,w;
- };
- int n,m;
- vector<arc>g[maxn];
- bool done[maxn];
- int d[maxn];
- priority_queue<pii,vector< pii >,greater< pii > >q;
- void dj() {
- int i,j;
- for(i = ; i <= n; i++) {
- done[i] = false;
- d[i] = INF;
- }
- d[] = ;
- while(!q.empty()) q.pop();
- q.push(make_pair(d[],));
- while(!q.empty()) {
- pii u =q.top();
- q.pop();
- int x = u.second;
- if(done[x]) continue;
- done[x] = true;
- for(i = ; i < g[x].size(); i++) {
- j = g[x][i].to;
- if(d[j] > d[x] + g[x][i].w) {
- d[j] = d[x]+g[x][i].w;
- q.push(make_pair(d[j],j));
- }
- }
- }
- }
- int main() {
- int i,j,u,v,w;
- while(scanf("%d%d",&n,&m),n||m) {
- for(i = ; i <= n; i++)
- g[i].clear();
- for(i = ; i < m; i++) {
- scanf("%d%d%d",&u,&v,&w);
- g[u].push_back((arc) {v,w});
- g[v].push_back((arc) {u,w});
- }
- dj();
- printf("%d\n",d[n]);
- }
- return ;
- }
Bellman—Ford算法:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <vector>
- #include <queue>
- #include <climits>
- #include <algorithm>
- #include <cmath>
- #define LL long long
- #define INF 0x3f3f3f
- using namespace std;
- #define pii pair<int,int>
- const int maxn = ;
- struct arc {
- int u,v,w;
- } g[maxn];
- int n,m,cnt;
- int d[maxn];
- void bf() {
- int i,j;
- for(i = ; i <= n; i++)
- d[i] = INF;
- d[] = ;
- bool flag;
- for(i = ; i < n; i++) {
- flag = true;
- for(j = ; j < cnt; j++)
- if(d[g[j].v] > d[g[j].u]+g[j].w){
- d[g[j].v] = d[g[j].u]+g[j].w;
- flag = false;
- }
- if(flag) break;
- }
- }
- int main() {
- int i,u,v,w;
- while(scanf("%d%d",&n,&m),n||m) {
- for(cnt = i = ; i < m; i++) {
- scanf("%d%d%d",&u,&v,&w);
- g[cnt++] = (arc) {u,v,w};
- g[cnt++] = (arc) {v,u,w};
- }
- bf();
- printf("%d\n",d[n]);
- }
- return ;
- }
spfa算法版:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <vector>
- #include <queue>
- #include <climits>
- #include <algorithm>
- #include <cmath>
- #define LL long long
- #define INF 0x3f3f3f
- using namespace std;
- #define pii pair<int,int>
- const int maxn = ;
- struct arc {
- int to,w;
- };
- vector<arc>g[maxn];
- queue<int>q;
- int n,m,d[maxn];
- bool done[maxn];
- void spfa() {
- int i,j;
- for(i = ; i <= n; i++){
- d[i] = INF;
- done[i] = false;
- }
- d[] = ;
- while(!q.empty()) q.pop();
- done[] = true;
- q.push();
- while(!q.empty()){
- int temp = q.front();
- q.pop();
- done[temp] = false;
- for(i = ; i < g[temp].size(); i++){
- j = g[temp][i].to;
- if(d[j] > d[temp]+g[temp][i].w){
- d[j] = d[temp]+g[temp][i].w;
- if(!done[j]){
- q.push(j);
- done[j] = true;
- }
- }
- }
- }
- }
- int main() {
- int i,u,v,w;
- while(scanf("%d%d",&n,&m),n||m) {
- for(i = ; i <= n; i++)
- g[i].clear();
- for(i = ; i < m; i++) {
- scanf("%d%d%d",&u,&v,&w);
- g[u].push_back((arc){v,w});
- g[v].push_back((arc){u,w});
- }
- spfa();
- printf("%d\n",d[n]);
- }
- return ;
- }
图论trainning-part-1 A. 最短路的更多相关文章
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 图论算法(三) 最短路SPFA算法
我可能要退役了…… 退役之前,写一篇和我一样悲惨的算法:SPFA 最短路算法(二)SPFA算法 Part 1:SPFA算法是什么 其实呢,SPFA算法只是在天朝大陆OIers的称呼,它的正统名字叫做: ...
- 图论算法(二)最短路算法:Floyd算法!
最短路算法(一) 最短路算法有三种形态:Floyd算法,Shortset Path Fast Algorithm(SPFA)算法,Dijkstra算法. 我个人打算分三次把这三个算法介绍完. (毕竟写 ...
- NOIp 图论算法专题总结 (1):最短路、最小生成树、最近公共祖先
系列索引: NOIp 图论算法专题总结 (1) NOIp 图论算法专题总结 (2) NOIp 图论算法专题总结 (3) 最短路 Floyd 基本思路:枚举所有点与点的中点,如果从中点走最短,更新两点间 ...
- 对于dijkstra最短路算法的复习
好久没有看图论了,就从最短路算法开始了. dijkstra算法的本质是贪心.只适用于不含负权的图中.因为出现负权的话,贪心会出错. 一般来说,我们用堆(优先队列)来优化,将它O(n2)的复杂度优化为O ...
- NOIP主要考查范围
基本数据结构 栈 队列 数组 优先队列 中级数据结构 堆(大根堆,小根堆) 并查集和带权并查集 哈希表 高级数据结构 (可选学) 树状数组 线段树 各种其他树 字符串和相关内容 1.KMP 2.各种操 ...
- Codevs 2370 小机房的树
2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为 ...
- 2014年CCNU-ACM暑期集训总结
2014年CCNU-ACM暑期集训总结 那个本期待已久的暑期集训居然就这种.溜走了.让自己有点措手不及.很多其它的是对自己的疑问.自己是否能在ACM这个领域有所成就.带着这个疑问,先对这个暑假做个总结 ...
- NOIP经典基础模板总结
date: 20180820 spj: 距离NOIP还有81天 目录 STL模板: priority_queue 的用法:重载<,struct cmpqueue 的用法 stack 的用法vec ...
- 【SPOJ116】Intervals
题目大意:有 N 个区间,在区间 [a, b] 中至少取任意互不相同的 c 个整数.求在满足 N 个区间约束的情况下,至少要取多少个正整数. 题解:差分约束系统模板题. 差分约束系统是对于 N 个变量 ...
随机推荐
- 难道这就是gin中间件的原理,一个装饰者模式而已?
func wrapCtx(handler func(ctx *gin.Context)) gin.HandlerFunc { return func(c *gin.Context) { //获取请求的 ...
- Spring AOP初步总结(二)
该篇为Spring AOP的一个应用案例:系统日志 需求:将任何删除,更改或新增数据库的操作汇总到数据库中 步骤1:编写切面 @Aspect @Component public class SysLo ...
- spring mvc添加静态资源访问时@Controller无效的解决
web.xml中的url-pattern设置为/,添加mvc:resources访问静态资源时,@Controller无效的问题 web.xml: <servlet> <servle ...
- 常用验证函数isset()/empty()/is_numeric()函数
1) isset()用来检查变量是否设置,若变量存在且值不为NULL时为TRUE: 检查多个变量时变量要全部存在且值不为NULL时为TRUE: 若用函数unset()释放后再用isset()检测时为F ...
- FusionCharts 3.2.1 常用用法
一.XML格式 1.实例化一个FusionCharts 对象 var member_fund_count_pie = new FusionCharts("FusionCharts3.2.1/ ...
- 洛谷 P1732 活蹦乱跳的香穗子
题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现自己很无聊,于是她想了一个有趣的事情,每个格子最多只能经过1次,且每个格子都有其价值 跳的规则是这样的,香穗子可以向上下左右四个方向跳到相邻的格子,并且她 ...
- asp.net mvc集成log4net
第一步:在web项目的引用中添加log4net.dll,可以通过Nuget直接下载并安装: 第二步:在web项目的web.config配置文件的configuration节点内添加log4net节点, ...
- 数组、Math、JOSN总结
json对象: 1.数组有length属性[尽量使用for循环] 2.而json没有length属性[可以使用for...in...循环] 3.for in 不能遍历页面中的节点对象. for ( v ...
- DatePicker 注意点 1.不用v-model 用:value 2.配合on-change进行回调 3.初始值 当天的用 (new Date()).toLocaleDateString().replace(/\//g, '-')
<DatePicker :value="formData.date" type="date" format="yyyy-MM-dd" ...
- 三、绘图和可视化之matplotlib
#matplotlib简单绘图之plot import matplotlib.pyplot as plt a=[1,2,3] b=[10,2,30] plt.plot(a)#纵坐标为a的值,横坐标为a ...