A. 最短路

Time Limit: 1000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

 
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的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 解题:最短路。。。
 #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. 最短路的更多相关文章

  1. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  2. 图论算法(三) 最短路SPFA算法

    我可能要退役了…… 退役之前,写一篇和我一样悲惨的算法:SPFA 最短路算法(二)SPFA算法 Part 1:SPFA算法是什么 其实呢,SPFA算法只是在天朝大陆OIers的称呼,它的正统名字叫做: ...

  3. 图论算法(二)最短路算法:Floyd算法!

    最短路算法(一) 最短路算法有三种形态:Floyd算法,Shortset Path Fast Algorithm(SPFA)算法,Dijkstra算法. 我个人打算分三次把这三个算法介绍完. (毕竟写 ...

  4. NOIp 图论算法专题总结 (1):最短路、最小生成树、最近公共祖先

    系列索引: NOIp 图论算法专题总结 (1) NOIp 图论算法专题总结 (2) NOIp 图论算法专题总结 (3) 最短路 Floyd 基本思路:枚举所有点与点的中点,如果从中点走最短,更新两点间 ...

  5. 对于dijkstra最短路算法的复习

    好久没有看图论了,就从最短路算法开始了. dijkstra算法的本质是贪心.只适用于不含负权的图中.因为出现负权的话,贪心会出错. 一般来说,我们用堆(优先队列)来优化,将它O(n2)的复杂度优化为O ...

  6. NOIP主要考查范围

    基本数据结构 栈 队列 数组 优先队列 中级数据结构 堆(大根堆,小根堆) 并查集和带权并查集 哈希表 高级数据结构 (可选学) 树状数组 线段树 各种其他树 字符串和相关内容 1.KMP 2.各种操 ...

  7. Codevs 2370 小机房的树

    2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为 ...

  8. 2014年CCNU-ACM暑期集训总结

    2014年CCNU-ACM暑期集训总结 那个本期待已久的暑期集训居然就这种.溜走了.让自己有点措手不及.很多其它的是对自己的疑问.自己是否能在ACM这个领域有所成就.带着这个疑问,先对这个暑假做个总结 ...

  9. NOIP经典基础模板总结

    date: 20180820 spj: 距离NOIP还有81天 目录 STL模板: priority_queue 的用法:重载<,struct cmpqueue 的用法 stack 的用法vec ...

  10. 【SPOJ116】Intervals

    题目大意:有 N 个区间,在区间 [a, b] 中至少取任意互不相同的 c 个整数.求在满足 N 个区间约束的情况下,至少要取多少个正整数. 题解:差分约束系统模板题. 差分约束系统是对于 N 个变量 ...

随机推荐

  1. 用vue.js重构订单计算页面

    在很久很久以前做过一个很糟糕的订单结算页面,虽然里面各区域(收货地址)使用模块化加载,但是偶尔会遇到某个模块加载失败的问题导致订单提交的数据有误. 大致问题如下: 1. 每个模块都采用usercont ...

  2. python经典一百道习题(转自奶酪博客)

    无论学习哪门计算机语言,只要把100例中绝大部分题目都做一遍,就基本掌握该语言的语法了. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #Filena ...

  3. silverlight GPS监控,视频监控界面

    周末闲着自己做了个玩玩

  4. Easyui combobox如何默认选中第一项???

    以下代码可以实现combobox默认选中第一项,在实际开发中我们可能会用到! // 处理combobox默认选中的问题 <input id="user_type" class ...

  5. SQL SERVER 2008 系列问题:无法访问,混合模式

    转载请注明:http://www.cnblogs.com/dachen408/p/7878494.html 使用本机服务器名'.'登录,使用windows模式: 1.修改登录模式为混合模式:右键服务器 ...

  6. eclipse 的project explorer问题,这个怎样把localFileSystem去掉,

    这个非常简单 把勾去掉就可以了

  7. TCP的三次握手与四次挥手详解

    TCP的三次握手与四次挥手是TCP创建连接和关闭连接的核心流程,我们就从一个TCP结构图开始探究中的奥秘  序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序 ...

  8. shell脚本,awk 根据文件某列去重并且统计该列频次。

    a文件为 a a a s s d .怎么把a文件变为 a s d .怎么把a文件变为 a a a s s d 解题方法如下: 解题思路 [root@localhost study]# awk 'NR= ...

  9. shell脚本,如何监控mysql数据库。

    [root@localhost wyb]# cat jkmysql #!/bin/bash status=`/etc/init.d/mysqld status|grep running|wc -l` ...

  10. javase(7)_Objcet类

    一.Object源代码 class Object { private static native void registerNatives(); static {registerNatives();} ...