Dijkstra算法:POJ No 3268 Silver Cow Party
题目: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的更多相关文章
- (poj)3268 Silver Cow Party 最短路
Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...
- 【POJ】3268 Silver Cow Party
题目链接:http://poj.org/problem?id=3268 题意 :有N头奶牛,M条单向路.X奶牛开party,其他奶牛要去它那里.每头奶牛去完X那里还要返回.去回都是走的最短路.现在问这 ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
随机推荐
- Java 将数字转为16进制,然后转为字符串类型
public class ArrayTest3 { public static void main(String[] args){ System.out.println(toHex(60)); } / ...
- 新手使用github过程记录
初次接触github,记录下我的使用过程.一开始确实有些懵,但好在网上这类的教程有很多,过程也很详细易懂,按照网上的教程走完全没问题,感谢无私分享辛苦整理的各位前辈们. 注册github账号 创建一个 ...
- lintcode-413-反转整数
413-反转整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 标签 整数 ...
- C++ Primer Plus学习:第五章
C++入门第五章:循环和关系表达式 for循环 for循环的组成部分 设置初始值. 执行测试,看循环是否应该继续执行. 执行循环操作. 更新用于测试的值. 以上操作由括号括起,每个部分均是一个表达式, ...
- Maven实现项目构建直接部署Web项目到Tomcat
Maven实现项目构建直接部署Web项目到Tomcat配置如下: 1.Tomcat的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <ro ...
- PHP中面向对象编程思想的3个特征
面向对象编程思想的3个特征: 封装: 无非是一个大的指向思想,目的是为了将一个类设计得更为健壮! 其基本做法是: 尽可能地将一个类的成员私有化,只开放那些必不可少的对外的属性或方法,能private的 ...
- 常用的Redis客户端的并发模型(转)
伪代码模型 # get lock : timestamp = current Unix time + lock = SETNX lock.foo timestamp or (now() > ...
- 【Django】Django—Form两种解决表单数据无法动态刷新的方法
一.无法动态更新数据的实例 1. 如下,数据库中创建了班级表和教师表,两张表的对应关系为“多对多” from django.db import models class Classes(models. ...
- HDU5669-Road
题意 给一个\(n\)个点的图,标号为\(1\)到\(n\),进行\(m\)次连边\((a,b,c,d,w)\): for i in range[a,b]: for j in range[c,d]: ...
- BZOJ3620 似乎在梦中见过的样子(kmp)
不是很懂为什么数据范围要开的这么诡异,想到正解都不敢写.用类似NOI2014动物园的方法,对每个后缀求出类似next的数组即可. #include<iostream> #include&l ...