一、题面

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. 在Linux下配置.net网站

    一.Linux安装 1.1 Linux环境 本篇文章选择VMWare虚拟机安装Linux,使用的Linux是CentOS-7.可以在百度上自行下载一个VMWare和CentOS-7镜像,建议使用最新版 ...

  2. 使用第三方库连接MySql数据库:PyMysql库和Pandas库

    使用PyMysql库和Pandas库链接Mysql 1 系统环境 系统版本:Win10 64位 Mysql版本: 8.0.15 MySQL Community Server - GPL pymysql ...

  3. functions函数插件的定义和使用

    创建Smarty插件:在插件目录(plugins)里新建文件 类型.插件名.php文件,然后插件方法名字书写规范: smarty_类型_插件名([...]){}在模板(tpl文件)中调用插件时格式{插 ...

  4. 使用Selenium&PhantomJS的方式爬取代理

    前面已经爬取了代理,今天我们使用Selenium&PhantomJS的方式爬取快代理 :快代理 - 高速http代理ip每天更新. 首先分析一下快代理,如下 使用谷歌浏览器,检查,发现每个代理 ...

  5. Qt资源整理ING

    QCustomPlot:图表库,开源, 链接地址http://www.qcustomplot.com/index.php/download 一些Qt的开发库:http://qt-project.org ...

  6. static在C和C++里各代表什么含义

    转自:http://blog.csdn.net/wanglongfei_hust/article/details/10011503 static关键字有三种使用方式,其中前两种只指在C语言中使用,第三 ...

  7. 公司web安全等级提升

    目录 背景介绍 目录 针对国调的初次测试结果 解决方案 二次测试 关于XSS 什么是XSS 基本防御 代码防御 xss代码辑录 说明 参考文章 版本记录 背景介绍 公司的一个web数据展示系统,本来是 ...

  8. Reconstruction(三维重建)文件被修改

    修改内容: 该函数被修改了一部分,然后修改中止了,可能是牵一发而动全身,导致中止.无论什么原因,这个Reconstruction.cpp文件是唯一被修改的文件了.如果没有被修改该多好!!!!!! 如何 ...

  9. 深入理解java虚拟机(十二) Java 语法糖背后的真相

    语法糖(Syntactic Sugar),也叫糖衣语法,是英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语.指的是,在计算机语言中添加某种语法,这些语法糖虽然不会对语言 ...

  10. Python + HTMLTestRunner + smtplib 完成测试报告生成及发送测试报告邮件

    一下代码是自己结合教材,并结合以往用到的实例编写的代码,可以做为参考 import smtplib from email.mime.text import MIMEText from email.mi ...