POJ_3268 Silver Cow Party 【最短路】
一、题面
二、分析
该题的意思就是给定了一个由每个节点代表农场的有向图,选定一个农场X办party,其余农场的都要去,每个农场的cow都走最短路,走的时间最久的cow耗时多少。
了解题意后,最开始想的是直接用floyd,但是复杂度已经到10的9次方了。这题比较特殊的一点就是无论是回来还是去都与X这个点有关,所以,当我们思考去求X到其他点的最短路径时,即根据输入的图求,其实就是在求返回的时长。如果我们把输入的图的路径的方向都反一下,即$a-b$变成$b-a$,这样相当于就是求其他所有点到X的时长了,两个对应相加求最大值就是最终的结果了。求的时候用dijkstra即可。
三、AC代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; typedef pair<int, int> P;
const int MAXN = 1e3;
const int INF = 0x3fffffff;
int map[MAXN+][MAXN+], map2[MAXN+][MAXN+];
int dist[MAXN+], dist2[MAXN+];
int N, M, X; void Max(int &a, int b)
{
if(a < b)
a = b;
} void dijkstra(int s, int graph[][MAXN+], int d[])
{
priority_queue<P, vector<P>, greater<P> > pq;
fill(d, d+N, INF);
d[s] = ;
pq.push(P(, s));
while(!pq.empty())
{
P t = pq.top();
pq.pop();
if(d[t.second] < t.first)
continue;
for(int i = ; i < N; i++)
{
if(d[i] > d[t.second] + graph[t.second][i])
{
d[i] = d[t.second] + graph[t.second][i];
pq.push(P(d[i], i));
}
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b, c, Ans;
scanf("%d %d %d", &N, &M, &X);
for(int i = ; i < N; i++)
{ for(int j = ; j < N; j++)
{
map[i][j] = INF;
map2[j][i] = INF;
}
map[i][i] = ;
map2[i][i] = ;
}
for(int i = ; i < M; i++)
{
scanf("%d %d %d", &a, &b, &c);
map[a-][b-] = c;
map2[b-][a-] = c;
}
dijkstra(X-, map, dist);
dijkstra(X-, map2, dist2);
Ans = ;
for(int i = ; i < N; i++)
{
Max(Ans, dist[i] + dist2[i]);
}
printf("%d\n", Ans);
}
POJ_3268 Silver Cow Party 【最短路】的更多相关文章
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ3268 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算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- (poj)3268 Silver Cow Party 最短路
Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...
- 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 ...
- TZOJ 1693 Silver Cow Party(最短路+思维)
描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big ...
- B - B Silver Cow Party (最短路+转置)
有n个农场,编号1~N,农场里奶牛将去X号农场.这N个农场之间有M条单向路(注意),通过第i条路将需要花费Ti单位时间.选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间 ...
- Silver Cow Party(最短路,好题)
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
随机推荐
- jquery简单ajax示例_读取json文件数据
来自于<jquery权威指南> -------------------------------------- 点击button后,获取到json文件数据,显示如下: Json文件: [ { ...
- 配置GIT DIFF/MERGE TOOL
关闭prompt backup git config --global difftool.prompt false git config --global mergetool.prompt false ...
- Photo4
Story: 我手捧玫瑰,一个人,走在桥上.桥下是波澜壮阔的大海,一不小心,我就有失足的危险.海鸟的低鸣在我耳际盘旋着,浪汹涌,仿佛要把我吞噬掉.你也许奇怪,为何我一人手捧玫瑰走在桥上.只因,女骑从来 ...
- linux 删除文件,某个文件例外
# shopt -s extglob (打开extglob模式) # rm -fr !(file1)
- C#记录程序运行时间
主要:using System.Diagnostics;当中有Stopwatch类: 介绍如下: // 摘要: // 提供一组方法和属性,可用于准确地测量运行时间. public class Stop ...
- 如何Android中加入扫描名片功能
要想实现android手机通过扫描名片,得到名片信息,可以使用脉可寻提供的第三方SDK,即Maketion ScanCard SDK,脉可寻云名片识别服务.他们的官方网站为http://www.mak ...
- ssh关于含有外键的传值中无法识别正确的action的原因和解决办法
在含有外键的表中,要保存一个值到这个外键时:逻辑思路:需要先将jsp页面的值传到相应的action中,在这个action中需要引入这个外键的实体层和DAO层(DAO层只需set方法),在执行函数中对于 ...
- Javascript与数据结构系列(一)——栈的实现
栈的实现 实现一个栈,当务之急是决定存储数据的底层数据结构.这里采用的是数组. 我们的实现以定义 Stack 类的构造函数开始: function Stack() { this.dataStore = ...
- ibatis源码学习3_源码包结构
ibatis的技术是从xml里面字符串转换成JAVA对象,对象填充JDBC的statement查询,然后从resultset取对象返回,另外利用ThreadLocal实现线程安全,JDBC保证了事务控 ...
- Linux RPM学习笔记
RPM(RedHat Package Manager) rp-pppoe-3.1-5.i386.rpm软件名称-版本号-编译次数-适合的硬件平台.扩展名 xxx-devel.rpm开发使用 xxx.n ...