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. 用户登陆界面(jquery)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 【详解】JS中的作用域、闭包和回收机制

    在讲解主要内容之前,我们先来看看JS的解析顺序,我们惯性地觉得JS是从上往下执行的,所以我们要用一个变量来首先声明它,来看下面这段代码: alert(a); var a = 1; 大家觉得这段代码有什 ...

  3. Fragment(一)--Fragment用法常见问题

    fragment notes fragment相关内容包括 基本定义与使用 回退栈内部实现 fragment通信(与activity 与fragment) DialogFragment VP + Fr ...

  4. Selenium私房菜系列5 -- 第一个Selenium RC测试案例

    <Selenium简介>中讲过,Selenium RC支持多种语言编写测试案例,如:C#,Python.在工作中,我倾向于是用Python这类动态语言编写测试案例,因为这样的测试案例无需编 ...

  5. Azure 项目构建 – 部署 Drupal 网站

    通过完整流程详细介绍了如何通过 Azure Web 应用. MySQL DB on Azure 等服务在 Azure 平台上快速搭建 Drupal 服务器,并将其连接到 MySQL 数据库. 此系列的 ...

  6. mybatis insert、update 、delete默认返回值解释与如何设置返回表主键

    在使用mybatis做持久层时,insert.update.delete,sql语句默认是不返回被操作记录主键的,而是返回被操作记录条数: 那么如果想要得到被操作记录的主键,可以通过下面的配置方式获取 ...

  7. HDU 5469 Antonidas (树形DP,暴力)

    题意: 给一棵n节点的树图,每个点都是一个小写字母,要求找到两个点(a,b),从a->b的路径上形成了一个字符串为s.给出s,问是否存在这样的点对. 思路: 考虑一个点,要么从该点出发,要么在该 ...

  8. HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法

    思路 标记最小的最后的位置  放在第一位 标记位置之前按left值小的左方大的右方 标记位置之后按顺序放在最后 不多说先贴上销毁内存的wa代码 销毁内存的wa代码 #include<cstdio ...

  9. 编程中什么是「Context(上下文)」?

    https://www.zhihu.com/question/26387327 每一段程序都有很多外部变量.只有像Add这种简单的函数才是没有外部变量的.一旦你的一段程序有了外部变量,这段程序就不完整 ...

  10. Xcode5 如何添加一个Github/Repository 并且Checkout

    1. 添加一个Account  也就是添加一个 Repository. In Xcode, choose Xcode > Preferences, and click Accounts. Pre ...