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 题解 《挑战程序设计竞赛》的更多相关文章
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- POJ 3268 Silver Cow Party 最短路径+矩阵转换
Silver Cow Party Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
随机推荐
- SQL Server 2000 创建角色,登陆用户,安全用户,批量授予权限
前言 我在2011年刚入门学习的时候,是从 SQL Server 2008 开始学的,再加上这些年较少接触 SQL Server 2000,因此对它不是很熟. 之前都是在 SQL Server 200 ...
- Django笔记四十三之使用uWSGI部署Django系统
本文首发于公众号:Hunter后端 原文链接:Django笔记四十三之使用uWSGI部署Django系统 目前部署 Django 的方式一般来说是使用 Nginx + uWSGI + Django 来 ...
- Hive数据库数据表元数据导出脚本
表结构导出 ## @Title Hive库表元数据导出脚本 ## @Author changxy #!/bin/bash ##############注意修改Hive连接信息############# ...
- springboot实现反向代理,动态代理目标地址
网上找了很多文章,各种照搬,只能自己实现 基于开源项目HTTP-Proxy-Servlet实现 开源项目地址:https://github.com/mitre/HTTP-Proxy-Servlet 1 ...
- 我理解的null和“ “,希望大家给予更正。
我认为null是零的意思,就是没有任何的东西,比如说文件的读取中,没有任何的东西就是null,刚刚用new String创建的String里面有一个引用,所以它不为null,如果用String h=& ...
- [scrapy]一个简单的scrapy爬虫demo
一个简单的scrapy爬虫demo 爬取豆瓣top250的电影名称+电影口号 使用到持久化流程: 爬虫文件爬取到数据后,需要将数据封装到items对象中. 使用yield关键字将items对象提交给p ...
- 数据仓库主流开发语言——SQL
数仓开发语言概述 SQL语言介绍 数仓与SQL 结构化数据 二维表结构 SQL语法分类
- python、numpy计算不同文档下的词的TF-IDF值以及进行文档相似度匹配(实战)
TF-IDF 计算公式(一个词的 tf-idf 值在不同文档,它的值也不同): 1.根据已有的原始数据,只展示了前5片文档,content是文档内容,s_words是通过jieba分词将文档划分成了若 ...
- ElasticSearch 命令执行漏洞
漏洞编号:CVE-2014-3120 漏洞详情 CVE编号 CVE-2014-3120 漏洞级别 中危6.8 标题 Elasticsearch默认配置允许动态脚本执行漏洞 披露时间 2014/07/2 ...
- python tkinter使用(五)
python tkinter使用(五) 本篇文章讲述tkinter 中treeview的使用 Treeview是一个多列列表框,可以显示层次数据. #!/usr/bin/python3 # -*- c ...