kuangbin_ShortPath A (POJ 2387)
最短路模板题 但是其实很费时间 因为要看明白dij floyd 以及 dij优化 spfa优化 交了三次 大概是理解了
不过涉及到priority_queue的重载运算符问题 以后要在C++里面好好看看 现在不理解
Dijkstra ver:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;
typedef pair<int, int> pii; int size, head[], point[], next[], val[];
int dis[], t, n; void add(int from, int to, int value)
{
point[size] = to;
next[size] = head[from];
val[size] = value;
head[from] = size++;
} struct cmp{
bool operator () (pii a, pii b){
return a.first > b.first;
}
}; void dijkstra(int s)
{
memset(dis, 0x3F, sizeof dis);
priority_queue<pii, vector<pii>, cmp> q;
q.push(make_pair(, s));
dis[s] = ;
while(!q.empty()){
pii u = q.top();
q.pop();
if(u.first > dis[u.second]) continue;
for(int i = head[u.second]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > u.first + val[i]){
dis[j] = u.first + val[i];
q.push(make_pair(dis[j], j));
}
}
}
} int main()
{
while(~scanf("%d%d", &t, &n)){
size = ;
memset(head, -, sizeof head);
for(int i = ; i <= t; i++){
int from, to, value;
scanf("%d%d%d", &from, &to, &value);
add(from, to, value);
add(to, from, value);
}
dijkstra();
printf("%d\n", dis[n]);
}
return ;
}
Spfa ver:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; int head[], point[], next[], val[], size; void add(int from, int to, int value)
{
point[size] = to;
val[size] = value;
next[size] = head[from];
head[from] = size++; point[size] = from;
val[size] = value;
next[size] = head[to];
head[to] = size++;
} void spfa(int s, int t)
{
int dis[];
bool vis[];
memset(dis, 0x3f, sizeof dis);
memset(vis, false, sizeof vis);
queue<int> q;
dis[s] = ;vis[s] = true;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > dis[u] + val[i]){
dis[j] = dis[u] + val[i];
if(!vis[j]){
q.push(j);
vis[j] = true;
}
}
}
}
printf("%d\n", dis[t]);
} int main()
{
int t, n;
memset(head, -, sizeof head);
scanf("%d%d", &t, &n);
while(t--){
int a, b, value;
scanf("%d%d%d", &a, &b, &value);
add(a, b, value);
}
spfa(, n);
return ;
}
kuangbin_ShortPath A (POJ 2387)的更多相关文章
- 链式前向星版DIjistra POJ 2387
链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- POJ 2387 Til the Cows Come Home(dijkstra裸题)
题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...
- POJ 2387 链式前向星下的SPFA
(POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
随机推荐
- 由于lightdm.conf 错误无法进入ubuntu 的办法
由于自己向默认登录GNOME桌面,所以修改了lightdm,由于参数错误,结果无法启动桌面? 这是需要进入shell界面: 1.选择cancel ,如果虚拟机下无法点击cancel按钮,可以使用快捷键 ...
- Matlab与C/C++联合编程之Matlab以MEX方式调用C代码(五)完整过程加示
如下为本人亲证代码: 一: 编译器的安装与配置(环境不同,显示结果不同) 要使用MATLAB编译器,用户计算机上应用事先安装与MATLAB适配的以下任何一种ANSI C/C++编译器: 5.0.6.0 ...
- form表单验证2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WebService的原理和过程
转自:http://blog.csdn.net/xiaoqiang081387/article/details/5694304 (一).XML WebService作用 XML WebService ...
- Android异步更新UI的四种方式
Android异步更新UI的四种方式 2015-09-06 09:23 segmentfault 字号:T | T 大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中 ...
- SQL调用系统存储过程整理
SQL系统存储过程用法整理: xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 xp_loginconfig --*报 ...
- CoreData的使用入门到精通
源码下载地址: http://download.csdn.net/download/huntaiji/6664567 一,创建项目文件--选择Empty Application 起名:CoreDat ...
- jQuery ajax传多个参数
ajax可以传送一个或多个参数到后台php中 <script> $(function(){ $("#sub_btn").click(function(){ var em ...
- 在Web.config中注册自定义控件
之前都是在每个页面的顶端注册用户控件或者是自定义控件,这样不简洁,而且麻烦. 现在只要在在web.config文件中声明,其他地方就可以直接使用前缀加控件如下黄色代码所示: <configura ...
- Magento在IE下登陆不了后台,在Firefox下正常
目前的解决办法如下: 方法一,用FF登陆后台,在 System—Configuration-Web-Session Cookie management....timeout 改为:86400 方法二: ...