POJ 3268 Silver Cow Party

Description

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2..
M+1: Line
i+1 describes road
i with three space-separated integers:
Ai,
Bi, and
Ti. The described road runs from farm
Ai to farm
Bi, requiring
Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
 
题目描述: 
一群牛分别从1~n号农场赶往x号农场参加聚会,农场与农场之间的路是单向的,在n个农场之间有m条路,给出 a ,b , t表示从a号农场到b号农场需要t时间。 每头牛都会选择最短的路,问来回路上花费时间最长的牛花费的时间是多少。
思路描述:
题意并不难理解,即求农场 X 到其余农场的最短距离与其余农场到X农场的最短距离,农场 X 到其余农场的最短距离一次dijkstra便可得到,但是求其余农场到X农场的最短距离,需要n次dijkstra方可得到,观察数据量便可得知普通的dijkstra算法难以满足要求,因为普通的dijkstra算法时间复杂度为O(n^2),加之n+1次调用,根据数据量必定超时,所以下面代码便对普通的dijkstra算法进行了优化,使之时间复杂度变为O(n*n*log n),便可满足题目的要求。而主要的实现是通过优先队列对边进行排序,从而先去了每次寻找最小边所花费的时间,具体细节详见代码如下:
 
代码实现:
 #include <set>
#include <map>
#include <stack>
#include <stdio.h>
#include <vector>
#include <utility>
#include<string.h>
#include <queue>
#include <iterator>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int inf = 0x3f3f3f3f;
int n,m,x;
struct edge
{
int u;
int v;
int cost;
} M[]; //存储原始边的信息;
struct N
{
int e; //每条边的权值;
int to; //终点;
};
vector<N> v[]; // 存储每条边的信息,下标为起始点;
int dis[]; // 起点到各个终点的最短距离;
void dijkstra(int s)
{
priority_queue<pair<int ,int>,vector<pair<int,int> >,greater<pair<int,int> > >q; //利用优先队列对里面的边从小到大进行排序;
for(int i = ;i<=;i++) v[i].clear(); //vector 清空;
fill(dis,dis+,inf); //初始化为最大值;
N n2;
for(int i = ;i<=m;i++) //将初始边加入到vector中;
{
n2.e = M[i].cost;
n2.to = M[i].v;
v[M[i].u].push_back(n2);
}
dis[s] = ;
//优化与实现:
q.push(pair<int,int>(,s));
while(!q.empty())
{
pair<int ,int> p = q.top();
q.pop();
int v1 = p.second;
if(dis[v1]<p.first) continue;
for(int i = ;i<v[v1].size();i++)
{
N n1 = v[v1][i];
if(dis[n1.to]>dis[v1]+n1.e)
{
dis[n1.to]=dis[v1]+n1.e;
q.push(pair<int,int>(dis[n1.to],n1.to));
}
}
}
}
int main()
{
int sum[];
while(~scanf("%d %d %d",&n,&m,&x))
{
memset(sum,,sizeof(sum));
for(int i = ;i<=m;i++) scanf("%d %d %d",&M[i].u,&M[i].v,&M[i].cost);
dijkstra(x);
for(int i = ;i<=n;i++) sum[i]+=dis[i];
for(int i = ;i<=n;i++)
{
dijkstra(i);
sum[i] += dis[x];
}
int max1 = ;
for(int i = ;i<=n;i++) if(sum[i]>max1) max1 = sum[i];
printf("%d\n",max1);
}
return ;
}
 
 
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
 
 
 

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。的更多相关文章

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

    描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...

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

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

  3. POJ 3268 Silver Cow Party 最短路

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

  4. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  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——————【最短路、Dijkstra、反向建图】

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

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

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

  8. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  9. POJ 3268 Silver Cow Party 单向最短路

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22864   Accepted: 1044 ...

随机推荐

  1. Python常见编程规范总结

    Pythonic定义 Python最常用的编码风格还是PEP8,详见:http://jython.cn/dev/peps/pep-0008/ Pythonic确实很难定义,先简单引用下<Pyth ...

  2. #linux 下Sublime的安装

    1.Download http://www.sublimetext.com/2 Installtion use tar  解压压缩包,这里我将包改了个名字,这样就不用写空格的转义字符了,改成Subli ...

  3. IOS NSBundle使用(访问文件夹)

    NSBundle的相关信息 1.一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2.利用mainBundle就可以访问软件资源包中的任何资源 3.模拟器应用程序的安装路径: ...

  4. 【洛谷】CYJian的水题大赛 解题报告

    点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...

  5. python_25_string

    name="my name is 齐志光qizhiguang" print(name.capitalize())#首字母变大写 print(name.count('i'))#统计字 ...

  6. PAT (Advanced Level) Practise - 1095. Cars on Campus (30)

    http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...

  7. Activiti学习记录(五)

    1.排他网关 说明: 1) 一个排他网关对应一个以上的顺序流 2) 由排他网关流出的顺序流都有个conditionExpression元素,在内部维护返回boolean类型的决策结果. 3) 决策网关 ...

  8. JSPatch - iOS 动态补丁

    JSPatch库,支持在线更新iOS应用,目前BDN项目中有用到,主要用来修复线上Crash和Bug 相关博文推荐: JSPatch – 动态更新iOS APP(这是JSPatch作者的博文) JSP ...

  9. vue axios 攻略

    Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特点: 从浏览器中创建 XMLHttpRequest 从 node.js 发出 http 请求 支持 ...

  10. js数组中去重对象

    var allCourses = new Array();var coursesId = new Array();function findCourses() { Courses.data().eac ...