最短路模板题 但是其实很费时间 因为要看明白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)的更多相关文章

  1. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

  2. 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 ...

  3. 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 ...

  4. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  5. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  6. 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 ...

  7. poj 2387 Til the Cows Come Home(dijkstra算法)

    题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...

  8. POJ 2387 Til the Cows Come Home(dijkstra裸题)

    题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...

  9. POJ 2387 链式前向星下的SPFA

    (POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...

随机推荐

  1. java基础之 http

    HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从HTTP服务器(Web服务器)请求 ...

  2. ASP.NET MVC4 & Entity Framework 6.0 IIS 部署出错解决方案

    博客地址 http://blog.csdn.net/foxdave 近期了解MVC4的时候弄了一个简单的小工程,使用Entity Framework作为Model,F5启动调试运行的时候没有问题,但是 ...

  3. 静态方法被override

    其实这并不是真正意义上的java override,因为如果在子类的方法上面加上@override编译不通过 而且如果使用父类引用指向子类实例,那么调用被改写的子类和父类都有的静态方法,执行的还是父类 ...

  4. dom对象操作Html,Css

    HTML: 1.不要再文档加载完使用document.write,这样会创建新的dom对象,原来的元素将被覆盖. 2.获取元素,通过getElementbyID; getElementbyTag(&q ...

  5. init()和deinit()

    一.初始化方法(init()) 1.定义:类初始化对象时所调用的方法 2.分类: (1)默认初始化方法 (2)便利初始化方法 (3)使用闭包 3.一些注意点: (1)方法固定名为init,没有返回值, ...

  6. c#多线程(UI线程,控件显示更新) Invoke和BeginInvoke 区别

    如果只是直接使用子线程访问UI控件,直接看内容三,如果想深入了解从内容一看起. 一.Control.Invoke和BeginInvoke方法的区别 先上总结: Control.Invoke 方法 (D ...

  7. application:didFinishLaunchingWithOptions:详解

    iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...

  8. Reason we use Camel

    Camel is mainly for integration purpose, in our project we also use it inside the single component t ...

  9. array_count_values函数

    array_count_values() 函数用于统计数组中所有值出现的次数. 本函数返回一个数组,其元素的键名是原数组的值,键值是该值在原数组中出现的次数. 例子 <?php $a=array ...

  10. https 三次握手

    1,客户端输入https网址,链接到server443端口: 2,服务器手中有一把钥匙和一个锁头,把锁头传递给客户端.数字证书既是公钥,又是锁头 3,客户端拿到锁头后,生成一个随机数,用锁头把随机数锁 ...