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 ...
随机推荐
- 软工1816 · Alpha冲刺(7/10)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 学会了POSTMAN的使用,对后端已经完成的接口进行了收发消息正确性的验证 推 ...
- Java中的网络编程-1
计算机网络:将分布在不同地区的计算机与专门的外部设备用通信线路互连成一个规模大.功能强的网络系统, 从而使众多计算机 可以方便的互相传递信息, 共享硬件.软件.数据信息等资源. 计算机网络的主要功能: ...
- Iterable,Iterator和forEach
Iterable Interface Iterable<T> 方法: Iterator<T> iterator() Returns an iterator over a set ...
- CodeForces Round #527 (Div3) C. Prefixes and Suffixes
http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...
- java分页算法
int totalPageNum = (totalRecord + pageSize - 1) / pageSize;
- fsocket发送post实现异步请求
function triggerRequest($url, $post_data = array(), $cookie = array()){ //可以通过POST或者GET传递一些参数给要触发的脚本 ...
- 利用css3的text-shadow属性实现文字阴影乳白效果
现在CSS3+html5的网页应用的越来越广泛了.很多网页中的字体同样可以用CSS3来实现炫酷的效果. 下面就介绍一下利用css3的text-shadow属性实现文字阴影乳白效果.这是在设计达人上面看 ...
- hdfs源码分析第一弹
1. hdfs定义 HDFS is the primary distributed storage used by Hadoop applications. A HDFS cluster primar ...
- HDU3710-Battle Over Cities
题意 给出一个\(n\)个点\(m\)条边的无向连通图,问删掉每一个点后的最小生成树权值和为多少(如果不存在最下生成树就输出inf). \(n\le 2\times 10^4,m\le 10^5\) ...
- BZOJ 2957 楼房重建(线段树区间合并)
一个显而易见的结论是,这种数字的值是单调递增的.我们修改一个数只会对这个数后面的数造成影响.考虑线段树划分出来的若干线段. 这里有两种情况: 1.某个线段中的最大值小于等于修改的数,那么这个线段的贡献 ...