非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了。

题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = 1e3 + ;
const int maxm = 1e5 + ;
const int inf = 0x3f3f3f3f;
struct edge{
int s, to, w, next;
} ed[maxm];
int n, m, x;
int mp[maxn][maxn], dis1[maxn], dis2[maxn];
bool vis[maxn];
inline int max( int a, int b ){
return a>b ? a:b;
} inline int min( int a, int b ){
return a<b ? a:b;
} inline int dij(){
//先求了去到x的最短路
memset( vis, , sizeof(vis) );
memset( dis1, inf, sizeof(dis1) );
dis1[x] = ;
for( int i=; i<n; i++ ){
int minid, MIN=inf;
for( int j=; j<=n ;j++ ) if( !vis[j] && MIN>dis1[j] ) MIN = dis1[minid=j];
vis[minid] = ;
for( int j=; j<=n; j++ ) if( !vis[j] ) dis1[j] = min(dis1[j], dis1[minid]+mp[minid][j]);
}
//接下来求回去的最短路
memset( vis, , sizeof(vis) );
memset( dis2, inf, sizeof(dis2) );
dis2[x] = ;
for( int i=; i<n; i++ ){
int minid, MIN = inf;
for( int j=; j<=n; j++ ) if( !vis[j] && MIN>dis2[j] ) MIN = dis2[minid=j];
vis[minid] = ;
for( int j=; j<=n; j++ ) if( !vis[j] ) dis2[j] = min( dis2[j], dis2[minid]+mp[j][minid] );
}
//遍历求出res
int res = -inf;
for( int i=; i<=n; i++ ) res = max( dis1[i]+dis2[i], res );
return res;
} int main(){
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(); //加速cin 和 cout读取速度,但是这样的话就不能使用scanf了
cin.tie();
cout.tie();
cin >> n >> m >> x;
memset( mp, inf, sizeof(mp) );
for( int i=; i<=n; i++ ) mp[i][i] = ;
for( int i=; i<m; i++ ){
int u, v, w;
cin >> u >> v >> w;
mp[u][v] = w;
}
cout << dij() << endl; return ;
}

poj3268 Silver Cow Party(最短路)的更多相关文章

  1. POJ3268 Silver Cow Party —— 最短路

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

  2. POJ3268 Silver Cow Party(dijkstra+矩阵转置)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 ...

  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 最短路—dijkstra算法的优化。

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

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

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

  6. POJ3268 Silver Cow Party Dijkstra最短路

    Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...

  7. POJ3268 Silver Cow Party【最短路】

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...

  8. poj3268 Silver Cow Party (SPFA求最短路)

    其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...

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

    Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...

随机推荐

  1. Appium基础之屏幕截图

    应用背景 在实际自动化项目运行过程中,很多时候App可以会出现各种异常,为了更好的定位问题,除了捕捉日志我们还需要对运行时的设备状态来进行截屏.从而达到一种“有图有真相”的效果. 截图方法 方法1 s ...

  2. SpringBoot读取Linux服务器某路径下文件\读取项目Resource下文件

    // SpringBoot读取Linux服务器某路径下文件 public String messageToRouted() { File file = null; try { file = Resou ...

  3. 【ARM-Linux开发】OpenACC并行编程实战笔记

    今年运气比较好,学了cuda之后,了解到了gpu的另两种使用语言opencl和openacc,  opencl(Open Computing Language ,开放计算语言)是面向异构系统的并行编程 ...

  4. IDEA和JIRA任务联动(redmine同理)

    IDEA和JIRA任务联动-操作流程详解 redmine和这个步骤类似,只是第一步选择的工具不一样 操作流程 添加jira配置,File->Setting-->tools-->Tas ...

  5. STL之iterator源码解析

    摘要 迭代器是一种行为类似指针的对象,本文介绍iterator与traits的关系,以及对traits内容的补充.包含stl_iterator.h的部分内容,并用c++11对其进行略微改写. iter ...

  6. CentOS 5 源

    # The mirror system uses the connecting IP address of the client and the # update status of each mir ...

  7. golang微服务框架go-micro 入门笔记2.3 micro工具之消息接收和发布

    本章节阐述micro消息订阅和发布相关内容 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go-micro环境, golang微服务框架go-mi ...

  8. 【实战经验】Xilinx时钟从普通IO输出问题

    Xilinx芯片的时钟信号从普通IO输出时,在map过程中会出错,对此有两种解决方案: 1.在ucf文件中,添加对应的约束文件: 例如[PIN "U0_1/clkout2_buf.O&quo ...

  9. SSM整合学习 三

    三:整合Mybatis 完整的项目如下 一:下载所需的jar包 <!--日志--><dependency> <groupId>log4j</groupId&g ...

  10. [BZOJ5197] [CERC2017]Gambling Guide

    [BZOJ5197] [CERC2017]Gambling Guide 题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5197 Solut ...