一、题面

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. BCompare 4重置试用天数

    BCompare安装后有30天试用期,试用结束后,你可以卸载重装,以重新获得30天试用天数. BCompare的使用天数记录保存在注册表中,如果不想每次重装,也可删除对应的注册表值来重置激活天数. 命 ...

  2. servletConfig的应用

    在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数. 当servlet配置了初始化参数后,web容器在创建servlet实例对象 ...

  3. bbs3

    第三天 昨日回顾: 1 验证码刷新 -$("#img_code")[0].src+="?" -本质就是向这个地址又发了一次请求 2 js中字符串拼接 -es5之 ...

  4. 编写高质量代码改善C#程序的157个建议——建议75:警惕线程不会立即启动

    建议75:警惕线程不会立即启动 现代的大多数操作系统都不是一个实时的操作系统,Windows系统也是如此.所以,不能奢望我们的线程能够立即启动.Windows内部会实现特殊的算法以进行线程之间的调度, ...

  5. 14、Semantic-UI之菜单样式

    14.1 基础菜单样式   在Semantic-UI中使用class="ui menu". 示例:定义基础菜单样式 <div class="ui menu" ...

  6. vsftpd安装与配置--研究tcp与防火墙

    vsftpd的配置文件 /etc/vsftpd/vsftpd.conf 主配置文件 /usr/sbin/vsftpd Vsftpd的主程序 /etc/rc.d/init.d/vsftpd 启动脚本 / ...

  7. Android-ContentProvider读取/新增/操作系统联系人数据

    想要访问Android操作系统的ContentProvider就需要明白以下原理: 在Android操作系统里面的 /packsges/目录: apps: 很多的系统应用,例如:联系人,浏览器,音乐播 ...

  8. 【C#】事件

    前言:CLR事件模式建立在委托的基础上,委托说调用回调方法的一种类型安全的方式. 我个人觉得事件本质就是委托,所以把委托弄清楚,只要知道事件基本语法就会使用了(如果说到线程安全,我个人觉得这个应该和线 ...

  9. Linux Qt 5.x 环境搭建

    Step 1 从Qt官网下载 qt-opensource-linux-x64...run 在linux命令行中给予文件可执行权限 $ chmod u+x qt-opensource-linux...r ...

  10. IO--RAID

    RAID IO计算 Raid 0 –每个磁盘的I/O计算= (读+写) /磁盘个数 Raid 1 --每个磁盘的I/O计算= [读+(2*写)]/2 Raid 5 --每个磁盘的I/O计算= [读+( ...