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 ...
随机推荐
- Android开发第二阶段(4)
今天:对按扭位置重新调整了一下布局了一下,改变了layout中见面的字体格式等等是其更美观. 明天:对图片的修改和替换.
- Java中switch可以接收的数据类型
Java支持的数据类型有五种 他们分别是: byte.char.short.int.enum: 以上是JDK1.6以前的版本. JDK1.7时,又增加了String, public class Tes ...
- SGU 181 X-Sequence(一题比较水的求模找规律)
E - X-Sequence Time Limit:500MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Submit ...
- Android工程方法数超过64k,The number of method references in a .dex file cannot exceed 64K.
最近将一个老的Eclipse项目转到Android Studio后,用gradle添加了几个依赖,项目可以make,但是一旦run就报错 Error:The number of method refe ...
- 关于解决乱码问题的一点探索之一(涉及utf-8和GBK)
在使用Visual Studio 2005进行MFC开发的时候,发现自动添加的注释变成了乱码.像这样: // TODO: ÔÚ´ËÌí¼ÓרÓôúÂëºÍ/»òµ÷ÓûùÀà 还有这样: // ...
- 浅谈 Sql Server 触发器
一.触发器概念 1.1.触发器特征 1.1.1.触发器是在对表进行增.删.改时,自动执行的存储过程.触发器常用于强制业务规则,它是一种高级约束,通过事件进行触发而被执行. ...
- exce族函数详解
exec函数族 函数族说明 fork() 函数用于创建一个新的子进程,该子进程几乎复制了父进程的全部内容,但是,这个新创建的子进程如何执行呢?exec 函数族就提供了一个在进程中启动另一个程序执行的方 ...
- 第110天:Ajax原生js封装函数
一.Ajax的实现主要分为四部分: 1.创建Ajax对象 // 创建ajax对象 var xhr = null; if(window.XMLHttpRequest){ xhr = new XMLHtt ...
- 【.Net+数据库】Unable to convert MySQL date/time value to System.DateTime
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
- 【BZOJ4200】【NOI2015】小园丁与老司机(动态规划,网络流)
[BZOJ4200][NOI2015]小园丁与老司机(动态规划,网络流) 题面 BZOJ权限题,洛谷链接 题解 一道二合一的题目 考虑第一问. 先考虑如何计算六个方向上的第一个点. 左右上很好考虑,只 ...