Silver Cow Party

Descriptions

给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出

Input

第1行:三个空格分隔的整数,分别为: N, M和 X 
行2 .. M +1:行 i +1描述具有三个空格分隔整数的道路 i: i, i和 i。所描述的道路从农场i运行 到农场 i,需要 i个时间单位来遍历。


Output

第1行:一个整数:所有奶牛最短路径中的最大值。


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

奶牛4直接进入该聚会(3个单位),并通过1号和3号农场(7个单位)返回,总共10个时间单位。
题目链接
 
10003用Floyd算法会超时,用Dijkstra算法,稍微改一下即可
 
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1000+5
#define P pair<int,int>//first最短路径second顶点编号
using namespace std;
int N,M,X;
struct edge
{
int to,cost;
edge(int to,int cost):to(to),cost(cost){}
};
vector<edge>G[Maxn];//G[i] 从i到G[i].to的距离为cost
int d[Maxn][Maxn];//d[i][j]从i到j的最短距离
void Dijk(int s)
{
priority_queue<P,vector<P>,greater<P> >q;//按first从小到大出队
for(int i=;i<=N;i++)
d[s][i]=INF;
d[s][s]=;
q.push(P(,s));
while(!q.empty())
{
P p=q.top();
q.pop();
int v=p.second;//点v
if(d[s][v]<p.first)
continue;
for(int i=;i<G[v].size();i++)
{
edge e=G[v][i];//枚举与v相邻的点
if(d[s][e.to]>d[s][v]+e.cost)
{
d[s][e.to]=d[s][v]+e.cost;
q.push(P(d[s][e.to],e.to));
}
}
}
}
int main()
{
IOS;
cin>>N>>M>>X;
for(int i=; i<M; i++)
{
int x,y,z;
cin>>x>>y>>z;
G[x].push_back(edge(y,z));
}
for(int i=;i<=N;i++)//枚举所有两点间的最短距离
Dijk(i);
int ans=;
for(int i=;i<=N;i++)
{
if(i==X)
continue;
ans=max(ans,d[i][X]+d[X][i]);
}
cout<<ans<<endl;
return ;
}

【POJ - 3268 】Silver Cow Party (最短路 Dijkstra算法)的更多相关文章

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

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

  2. 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 ...

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

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

  4. POJ 3268 Silver Cow Party 最短路

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

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

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

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

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

  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 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

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

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

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

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

随机推荐

  1. Mybatis延迟加载, 一级缓存、二级缓存

    延迟加载 概念:MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. (注意:MyBatis的延迟加载只 ...

  2. 搭建gitlab服务

    安装依赖 sudo yum install curl policycoreutils openssh-server openssh-clients sudo systemctl enable sshd ...

  3. 原生JS实现滑动验证功能

    一般很多网站都有滑动验证的功能,简单滑动验证的原理如下图所示: 主要理解思想就行 图中的代码可能和实际写的有所不同 HTML和CSS也可根据仔细的喜好就行修改 完整代码: <!DOCTYPE h ...

  4. 011——C#创建ecxel文件(附教程)

    (一)参考文献:[C#]创建表格(.xlsx)的典型方法 (二)视频教程:https://v.qq.com/x/page/t30068qfex5.html (三)下载地址:https://downlo ...

  5. Greenplum 监控segment是否正常

    在greenplum运行过程中,Segement很有可能因为压力大出现不可用的情况, 主备Segement发现了切换,或是主备Segement网络断开,数据不同步了.在 默认情况下,如果GreenPl ...

  6. PostgreSQL 学习手册-模式Schema

    一个数据库包含一个或多个命名的模式,模式又包含表.模式还包含其它命名的对象,包括数据类型.函数,以及操作符.同一个对象名可以在不同的模式里使用而不会导致冲突: 比如,schema1和myschema都 ...

  7. Crontab 定时任务格式参数

    crontab 定时执行 任务 格式: * * * * *-) 小时(-) 日(-) 月份(-) 星期(-6) 星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条 ...

  8. TensorFlow(十四):谷歌图像识别网络inception-v3下载与查看结构

    上代码: import tensorflow as tf import os import tarfile import requests #inception模型下载地址 inception_pre ...

  9. java.lang.ClassNotFoundException: org.springframework.kafka.core.KafkaAdmin

    <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring ...

  10. hbuilder连接模拟器进行联调(逍遥模拟器,MuMu模拟器,夜神模拟器)

    MuMu模拟器:7555 逍遥模拟器:21503 夜神模拟器:62001 1. 2. 3. 如果上诉方法不好使,可以重启模拟器以及hbuilder,有时可能连接中断,可以重新连接.