#include<iostream>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdio>
using namespace std;
#define MAXN 1004
#define INF 0x3f3f3f3f
/*
18 08
18 23
超时
18 24
18 33
超时
18 41 AC
有向图,所有结点中到终点来回距离的最大值
对每个节点求最短路得到to[MAXN],对终点求最短路的back[MAXN]
n3!超时!
只需将有向图反转一次(有向路径反向),对终点求两次Dijk即可
max(to+back)
*/
bool been[MAXN];
int lowcost[MAXN],g[MAXN][MAXN],m,n,x,ans=-;
int to[MAXN],back[MAXN];
void Read()
{
int x,y,d;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&x,&y,&d);
g[x][y] = d;
}
}
void Init()
{
memset(lowcost,INF,sizeof(lowcost));
memset(been,false,sizeof(been));
}
void Dijkstra(int beg,int tmp[])
{
Init();
lowcost[beg] = ;
for(int j=;j<n;j++)
{
int Min = INF,k = -;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]<Min)
{
Min = lowcost[i];
k = i;
}
}
if(k==-) break;
been[k] = true;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]>lowcost[k]+g[k][i])
{
lowcost[i] = lowcost[k]+g[k][i];
}
}
}
for(int i=;i<=n;i++)
{
if(i==beg) continue;
tmp[i] = lowcost[i];
}
}
int main()
{
scanf("%d%d%d",&n,&m,&x);
memset(g,INF,sizeof(g));
Read();
Dijkstra(x,back);
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
int t = g[i][j];
g[i][j] = g[j][i];
g[j][i] = t;
}
}
Dijkstra(x,to);
for(int i=;i<=n;i++)
{
ans = max(ans,back[i]+to[i]);
}
printf("%d\n",ans);
return ;
}

kuangbin专题最短路 D - Silver Cow Party的更多相关文章

  1. (最短路)Silver Cow Party --POJ--3268

    题目链接: http://poj.org/problem?id=3268 题意: 先求出所有牛到x的最短路,再求出x到所有牛的最短路,两者相加取最大值(单向图)(可以用迪杰斯特拉,SPFA) 迪杰斯特 ...

  2. Silver Cow Party(最短路,好题)

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

  3. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

  4. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

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

  5. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

  6. POJ 3268 Silver Cow Party 最短路

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

  7. POJ3268 Silver Cow Party —— 最短路

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

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

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

  9. poj3268 Silver Cow Party(最短路)

    非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. ...

随机推荐

  1. 洛谷P1010 幂次方

    题目描述 任何一个正整数都可以用2的幂次方表示.例如 137=2^7+2^3+2^0 同时约定方次用括号来表示,即a^b 可表示为a(b). 由此可知,137137可表示为: 2(7)+2(3)+2( ...

  2. 关于.Net中Process的使用方法和各种用途汇总(一):Process用法简介

    简介: .Net中Process类功能十分强大.它可以接受程序路径启动程序,接受文件路径使用默认程序打开文件,接受超链接自动使用默认浏览器打开链接,或者打开指定文件夹等等功能. 想要使用Process ...

  3. .Net实战之反射操作篇

    1.上一讲中描述了反射中常见的类,仅仅是描述类与反射之间的关系.   但是实际是对数据的操作,  在反射中,数据如何操作? [MyTable("T_UserInfo")] publ ...

  4. Elasticsearch--预匹配器

    当你对一个无限输入数据流进行操作并搜索特定事件的出现时,可以使用此模型.可以用于检测监控系统中的故障. 在新版本中的知识点位置https://www.elastic.co/guide/en/elast ...

  5. oracle in表达式参数支持最大上限1000个

    oracle in表达式参数支持最大上限1000个 方法是拆分为多个 col in ... or col in ... #region 解决大于1000的问题 private String getSu ...

  6. 联想 P70-t 免解锁BL 免rec Magisk Xposed 救砖 ROOT

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  7. 读《An Adaptable and Extensible Geometry Kernel》

    读<An Adaptable and Extensible Geometry Kernel> 利用Curiously Recurring Template Pattern替代虚函数 详细内 ...

  8. PHP7中session_start 使用注意事项,会导致浏览器刷时页面数据不更新

    //PHP7中session_start 使用注意事项, session_start([ 'cache_limiter' => 'private', //在读取完毕会话数据之后马上关闭会话存储文 ...

  9. maven引入的包无法使用 解决方法

    如果正常引入后在依赖中能够找到包,但是打不开 1.有可能是包下载不完整 把maven下载源由国外转成阿里镜像源找到 maven 的配置文件: settings.xml 文件: <mirror&g ...

  10. "ping: unknown host www.baidu.com"问题解决方式

    参考:https://blog.csdn.net/wbainngg123/article/details/51540535 在虚拟机VMware里选择桥接模式,并配置网络之后,发现ping ip地址可 ...