POJ 3268 Silver Cow Party

奶牛派对:有分别来自 N 个农场的 N 头牛去农场 X 嗨皮,农场间由 M 条有向路径连接。每头牛来回都挑最短的路走,求它们走的路的最大长度?

们其实都是“图”

最短路 dijkstra 解决任意两点最短路的变种

用floyd的话会TLE,\(1000^3\)复杂度果然不是盖的,另外X的编号记得减一,测试用例很恶心的,即使忘了减一也照样得出正确答案10,但是一提交就\(WA\),让我深深地感到了来自命题者的恶意:

//第一次用OJ宏定义,提交的时候注释掉前3行即可
#ifndef ONLINE_JUDGE
#pragma warning(disalbe : 4996)
#endif
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAX_V 1024
int d[MAX_V][MAX_V];//d[u][v]表示边e=(u,v)的权值,不存在的时候等于无穷大或者d[i][i] = 0
int V;//顶点数 void floyd() {
for (int k = 0; k < V; ++k)
for (int i = 0; i < V; ++i)
for (int j = 0; j < V; ++j)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
} ////////////////////////////////SubMain///////////////////////////////
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int M, X;
cin >> V >> M >> X;
--X;
memset(d, 0x3f, V * MAX_V * sizeof(int));
for (int i = 0; i < V; ++i)d[i][i] = 0;
while (M--) {
int A, B, T;
cin >> A >> B >> T;
--A;
--B;
d[A][B] = T;
}
floyd();
int ans = 0;
for (int i = 0; i < V; ++i)
ans = max(ans, d[i][X] + d[X][i]);
cout << ans << endl;
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif // !ONLINE_JUDGE
return 0;
}
////////////////////////////////End Sub///////////////////////////////

dijkstra是解决单源最短路问题的,稍微修改一下就可以支持任意两点最短路:

//#ifndef ONLINE_JUDGE
//#pragma warning(disable : 4996)
//#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <functional>
using namespace std;
#define MAX_V 1024 // 从顶点from指向顶点to的权值为cost的边
struct edge
{
int to, cost;
edge(){}
edge(int to, int cost) : to(to), cost(cost){}
}; // first 最短路径,second顶点编号
typedef pair<int, int> P; // 图
vector<edge> G[MAX_V]; // 最短距离
int d[MAX_V][MAX_V];
// V是顶点数,E是边数
int 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] = 0;
que.push(P(0, s)); while (!que.empty())
{
P p = que.top(); que.pop();
int v = p.second;
if (d[s][v] < p.first) continue;
for (int i = 0; i < G[v].size(); ++i)
{
edge e = G[v][i];
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));
}
}
}
} ///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int M, X;
cin >> V >> M >> X;
--X;
while (M--)
{
int A, B, T;
cin >> A >> B >> T;
--A;
--B;
G[A].push_back(edge(B, T));
}
for (int i = 0; i < V; ++i)
{
dijkstra(i);
} int ans = 0;
for (int i = 0; i < V; ++i)
{
if (i == X)
{
continue;
}
ans = max(ans, d[i][X] + d[X][i]);
} cout << ans << endl;
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return 0;
}
///////////////////////////End Sub//////////////////////////////////
Time 719ms
Memory 4736kB
Length 1651
Lang G++
Submitted 2020-05-14 11:15:13

不过上面那个实在太浪费,我只想计算起点或终点是X的路径最短路问题,可以在上面while (!que.empty())的循环里加入判断条件,还可以这么玩:

来一个反向图,交换边的起点和终点得到反向的有向图,两次dijkstra解决问题:

//#ifndef ONLINE_JUDGE
//#pragma warning(disable :4996)
//#endif // !ONLINE_JUDGE
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include<cstring>
#include <functional>
using namespace std;
#define MAX_V 10240 // 从顶点from指向顶点to的权值为cost的边
struct edge
{
int to, cost;
edge() {}
edge(int to, int cost) : to(to), cost(cost) {}
}; // first 最短路径,second顶点编号
typedef pair<int, int> P; // 图
vector<vector<edge> > G(MAX_V);
// 反向图
vector<vector<edge> > RG(MAX_V); // 最短距离
int d[MAX_V];
int rd[MAX_V];
// V是顶点数,E是边数
int V, E; // 求解从顶点s出发到所有点的最短距离
void dijkstra(int s)
{
priority_queue<P, vector<P>, greater<P> > que;
memset(d, 0x3f, sizeof(d));
d[s] = 0;
que.push(P(0, s)); while (!que.empty())
{
P p = que.top(); que.pop();
int v = p.second;
if (d[v] < p.first) continue;
for (int i = 0; i < G[v].size(); ++i)
{
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost)
{
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
} ///////////////////////////SubMain//////////////////////////////////
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int M, X;
cin >> V >> M >> X;
--X;
while (M--)
{
int A, B, T;
cin >> A >> B >> T;
--A;
--B;
G[A].push_back(edge(B, T));
RG[B].push_back(edge(A, T));
}
dijkstra(X);
G = RG;
memcpy(rd, d, sizeof(d));
dijkstra(X);
for (int i = 0; i < V; ++i)
{
d[i] += rd[i];
} cout << *max_element(d, d + V) << endl;
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return 0;
}
///////////////////////////End Sub//////////////////////////////////

优化以后Time降到了 235ms

POJ 3268 Silver Cow Party 题解 《挑战程序设计竞赛》的更多相关文章

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

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

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

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

  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)

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

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

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

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

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

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

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

  8. POJ 3268 Silver Cow Party 最短路径+矩阵转换

    Silver Cow Party Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

  9. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

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

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

随机推荐

  1. 火山引擎ByteHouse:如何优化ClickHouse物化视图能力?

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 近期,火山引擎 ByteHouse 升级了基于 ClickHouse 的物化视图能力,为解决数据量爆炸式增长带来的 ...

  2. Go切片是值传递还是引用传递?

    Go没有引用传递和引用类型!!! 很多人有个误区,认为涉及Go切片的参数是引用传递,或者经常听到Go切片是引用类型这种说法,今天我们就来说一下方面的问题. 什么是值传递? 将实参的值传递给形参,形参是 ...

  3. Android Studio 学习-第三章 Activity 第二组

    事先申明:所有android 类型的学习记录全部基于<第一行代码 Android>第三版,在此感谢郭霖老师的书籍帮助. 1.注册activity 在第一组中,我创建了一个activity, ...

  4. Go 语言区块链测试:实践指南

    引言 Go 语言在区块链开发中的应用日益增多,凭借其简洁的语法和强大的并发支持,成为开发区块链应用的热门选择.理解和实践 Go 语言的单元测试对于保证区块链应用的质量和稳定性至关重要. Go 单元测试 ...

  5. 【软件安装】vmware虚拟机安装完整教程(15.5版本)

     目录 一.基础介绍 二.准备工作(注意:如果自己下载不下来翻到最下面获取下载地址) 三.VMware下载与安装 VMware Workstation15.5新功能 注意: 一.基础介绍 VMware ...

  6. LeetCode1806:还原排列的最少操作步数(置换群 or 模拟)

    题意:题目的意思是,给定一个初始状态perm,然后对perm的每个元素按照上述的规则进行变换操作.问:perm经过多少次这种操作能够变回初始的perm. 解题思路:第一种方法就是模拟,一直变换,直到变 ...

  7. 【分享】推荐一个非常好用的redis远程连接工具

    推荐一个非常好用的redis远程连接工具 蓝奏云地址 https://wwsi.lanzoum.com/ig7xZ0xspf0h 密码:4vnz 二维码:

  8. Math数学工具类、向上取整,向下取整,四舍五入,最大值

    package com.guoba.math; public class MathTest { /* Math数学工具类,包含以下方法: .ceil() 向上取整 .floor() 向下取整 .rou ...

  9. zabbix 默认消息

    故障事件: {TRIGGER.NAME}监控状态: {TRIGGER.STATUS}报警严重性: {TRIGGER.SEVERITY}触发结果: {TRIGGER.URL}告警时间:{EVENT.DA ...

  10. java获取包下所有java类

    java获取包下所有java类 简单加载包下的类,注意简单编写非递归查找,自行实现递归查找即可 import java.io.File; import java.net.URL; import jav ...