题目:http://poj.org/problem?id=3268

题解:使用 priority_queue队列对dijkstra算法进行优化

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
using namespace std; const int MAX_V = + ;
const int INF = ; struct edge
{
int to, cost;
edge() {}
edge(int to, int cost) : to(to), cost(cost) {}
};
typedef pair<int, int> P; //first 最短路径, second顶点编号
vector<edge> G[MAX_V]; //图
int d[MAX_V][MAX_V]; //最短距离
int V, E, X; //V顶点数,E是边数 //求解从顶点s出发到所有点的最短距离
void dijkstra(int s)
{
//升序
priority_queue<P, vector<P>, greater<P> > que;
memset(d[s], 0x3f, MAX_V * sizeof(int));
d[s][s] = ;
que.push(P(, s)); //最短路径,顶点编号 while (!que.empty())
{
//每次出队最小的
P p = que.top(); que.pop();
int v = p.second; //编号 if (d[s][v] < p.first) continue; for (unsigned i = ; i < G[v].size(); ++i)
{
edge e = G[v][i]; //与v临边的顶点
// d[s][e.to]经过 v 再经过其他的临边
if (d[s][e.to] > d[s][v] + e.cost)
{
d[s][e.to] = d[s][v] + e.cost;
que.push(P(d[s][e.to], e.to));
}
}
}
} void solve()
{
cin >> V >> E >> X;
--X;
int s, t, ct;
while (E--)
{
cin >> s >> t >> ct;
--s; --t;
G[s].push_back(edge(t, ct));
} for (int i = ; i < V; i++) {
dijkstra(i);
} int ans = ;
for (int i = ; i < V; ++i) {
if (i == X) continue; ans = max(ans, d[i][X] + d[X][i]);
} printf("%d\n", ans);
} int main()
{
solve(); return ; }

Dijkstra算法:POJ No 3268 Silver Cow Party的更多相关文章

  1. (poj)3268 Silver Cow Party 最短路

    Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...

  2. 【POJ】3268 Silver Cow Party

    题目链接:http://poj.org/problem?id=3268 题意 :有N头奶牛,M条单向路.X奶牛开party,其他奶牛要去它那里.每头奶牛去完X那里还要返回.去回都是走的最短路.现在问这 ...

  3. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  4. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  6. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  9. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

随机推荐

  1. HDU 5656 CA Loves GCD 01背包+gcd

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5656 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  2. 第13章 学习shell script

    由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示 第一个script [root@localhost script]# cat -n sh01.sh #!/bin/bash ...

  3. java 抽象类&接口

    1,抽象类中有构造函数吗? 有,用于给子类对象进行初始化.   2,抽象关键字不可以和那些关键字共存? private 不行 static 不行 final 不行   final关键字: 1,fina ...

  4. ASLR/DEP绕过技术概览

    在经典的栈溢出模型中,通过覆盖函数的返回地址来达到控制程序执行流程(EIP寄存器),通常将返回地址覆盖为0x7FFA4512,这个地址是一条JMP ESP指令,在函数返回时就会跳转到这个地址去执行,也 ...

  5. iOS- 关于AVAudioSession的使用——后台播放音乐

    1.前言 •AVAudioSession是一个单例,无需实例化即可直接使用.AVAudioSession在各种音频环境中起着非常重要的作用 •针对不同的音频应用场景,需要设置不同的音频会话分类   1 ...

  6. 【C】多线程编程笔记

    1. pthread_create(pthread类型指针变量 ,NULL ,函数 ,函数参数[多个参数用结构体传]) 2. pthread_join(pthread类型指针变量, 返回一般为null ...

  7. PHP 在windows下配置sendmail,通过 mail() 函数发送邮件

    php mail()函数在windows不能用,需要安装sendmail. 1. 从http://glob.com.au下载sendmail.zip 2. 解压sendmail.zip到目录下(最好使 ...

  8. spring 整合 struts2 + Hibernate application配置文件(基于注解)

    下面是 application.xml 文件. <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  9. Javascript面向对象三大特性(封装性、继承性、多态性)详解及创建对象的各种方法

    Javascript基于对象的三大特征和C++,Java面向对象的三大特征一样,都是封装(encapsulation).继承(inheritance )和多态(polymorphism ).只不过实现 ...

  10. [USACO4.1]麦香牛块Beef McNuggets 题解报告

    题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是"劣质的包装".& ...