图论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 个变量 ...
随机推荐
- KVC/KVO 本质
KVO 的实现原理 KVO是关于runtime机制实现的 当某个类的对象属性第一次被观察时,系统就会在运行期动态地创建该类的一个派生类,在这个派生类中重写基类中任何被观察属性的setter方法.派生类 ...
- python深浅拷贝问题
Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果. 下面本文就通过简单的例子介绍一下这些概念之间的差别. 一.对象赋值 又叫变量对对象的引用 l ...
- sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL [SQL: u'ALTER TABLE address_scopes ADD COLUMN ip_version INTEGER NOT NULL']
root@hett-virtual-machine:~# su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neu ...
- CSS3制作的一款按钮特效
/*.btn { width:230px; height:70px; font-size:70px; font-weight:bold; overflow:hidden; font: "He ...
- WPF知识点全攻略02- WPF体系结构
WPF体系结构图: PersentationFramework.dll包含WPF顶层的类型,包括哪些表示窗口.面板以及其他类型控件的类型.他还实现了高层编程抽象,如样式.开发人员直接使用的大部分类都来 ...
- 常用的 Excel 函数
概述 Excel 学的好,函数不可少.接下来就了解常用的函数. 首先作下简要说明: 本文的内容大多从网上搜集并加以个人理解整理而来,由于初学,可能会出现错误,如有欢迎指出: 所用演示软件为免费丑陋的 ...
- Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...
http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...
- 打印两个有序链表的公共部分 【题目】 给定两个有序链表的头指针head1和head2,打印两个 链表的公共部分
简单题 package my_basic.class_3; public class Code_10_PrintCommonPart { public static class Node{ int v ...
- AR报表控件的常见问题汇总
1)字符串格式不正确 原因是建立报表时未使用分页报表 导致拖动数据为sum()表达式,sum运算字符串当然会出问题 2)超出索引 顾名思义 数据为空或不匹配 3)图片使用远程图片时记得把属性修改为远程 ...
- Quartz监听的端口
上海移通短信网关:556重庆移动短信网关:557消息中心后台维护服务:558网页订单数据同步服务:559基础数据同步程序:560短信数据扣除服务:565基础数据维护服务:589推送数据抓取服务:222 ...