一、题面

POJ3268

二、分析

该题的意思就是给定了一个由每个节点代表农场的有向图,选定一个农场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 【最短路】的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路

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

  2. POJ3268 Silver Cow Party —— 最短路

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

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

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

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

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

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

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

  6. 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 ...

  7. 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 ...

  8. B - B Silver Cow Party (最短路+转置)

    有n个农场,编号1~N,农场里奶牛将去X号农场.这N个农场之间有M条单向路(注意),通过第i条路将需要花费Ti单位时间.选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间 ...

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

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

随机推荐

  1. Ubuntu14.04下FTP服务器的搭建配置 标签: ubuntuftp服务器虚拟机 2017-06-13 15:24 161人阅读 评

    首先说明一下,我是在虚拟机中装的Ubuntu14.04,物理机是Win10,最初只是为了在两个系统间传输文件才在Ubuntu中安装了ftp服务器,从Windows端登陆其即可.最初也是按照网上的各种教 ...

  2. [SoapUI] 通过编程的方式设置当前的Environment

    testRunner.testCase.testSuite.project.setActiveEnvironment("Live")

  3. js闭包原理与例子[转]

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.闭包原理: 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域 ...

  4. Docker for mac安装

    Mac安装Docker docker下载地址: https://hub.docker.com/editions/community/docker-ce-desktop-mac docker for m ...

  5. Hadoop中Writable类

    1.Writable简单介绍 在前面的博客中,经常出现IntWritable,ByteWritable.....光从字面上,就可以看出,给人的感觉是基本数据类型 和 序列化!在Hadoop中自带的or ...

  6. (转)jQuery插件编写学习+实例——无限滚动

    原文地址:http://www.cnblogs.com/nuller/p/3411627.html 最近自己在搞一个网站,需要用到无限滚动分页,想想工作两年有余了,竟然都没有写过插件,实在惭愧,于是简 ...

  7. 机器学习及其matlab实现—从基础到实践

    第1周 MATLAB入门基础 第2周 MATLAB进阶与提高 第3周 BP神经网络 第4周 RBF.GRNN和PNN神经网络 第5周 竞争神经网络与SOM神经网络 第6周 支持向量机(Support ...

  8. requestAnimationFrame 定时器

    这个方法是通过递归调用同一方法来不断更新画面以达到动起来的效果,但它优于setTimeout/setInterval的地方在于它是由浏览器专门为动画提供的API,在运行时浏览器会自动优化方法的调用,并 ...

  9. 关于Markdown格式转PDF格式

    Markdown转PDF格式 个人使用的Markdown编辑平台:有道云笔记网页版 当我们编辑好自己的随笔以后,在网页的[客户端下载]下面有一个[更多]的圆形图标选项,点击后在菜单中有一处[打印]选项 ...

  10. Replication--分区+复制

    1>配置订阅表使用分区,在发布的项目属性中设置"复制分区方案"和"复制索引分区方案"为true,然后初始化订阅 2>在发布数据库上修改发布属性 -- ...