POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2..
M+1: Line
i+1 describes road
i with three space-separated integers:
Ai,
Bi, and
Ti. The described road runs from farm
Ai to farm
Bi, requiring
Ti time units to traverse.
Output
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
#include <set>
#include <map>
#include <stack>
#include <stdio.h>
#include <vector>
#include <utility>
#include<string.h>
#include <queue>
#include <iterator>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int inf = 0x3f3f3f3f;
int n,m,x;
struct edge
{
int u;
int v;
int cost;
} M[]; //存储原始边的信息;
struct N
{
int e; //每条边的权值;
int to; //终点;
};
vector<N> v[]; // 存储每条边的信息,下标为起始点;
int dis[]; // 起点到各个终点的最短距离;
void dijkstra(int s)
{
priority_queue<pair<int ,int>,vector<pair<int,int> >,greater<pair<int,int> > >q; //利用优先队列对里面的边从小到大进行排序;
for(int i = ;i<=;i++) v[i].clear(); //vector 清空;
fill(dis,dis+,inf); //初始化为最大值;
N n2;
for(int i = ;i<=m;i++) //将初始边加入到vector中;
{
n2.e = M[i].cost;
n2.to = M[i].v;
v[M[i].u].push_back(n2);
}
dis[s] = ;
//优化与实现:
q.push(pair<int,int>(,s));
while(!q.empty())
{
pair<int ,int> p = q.top();
q.pop();
int v1 = p.second;
if(dis[v1]<p.first) continue;
for(int i = ;i<v[v1].size();i++)
{
N n1 = v[v1][i];
if(dis[n1.to]>dis[v1]+n1.e)
{
dis[n1.to]=dis[v1]+n1.e;
q.push(pair<int,int>(dis[n1.to],n1.to));
}
}
}
}
int main()
{
int sum[];
while(~scanf("%d %d %d",&n,&m,&x))
{
memset(sum,,sizeof(sum));
for(int i = ;i<=m;i++) scanf("%d %d %d",&M[i].u,&M[i].v,&M[i].cost);
dijkstra(x);
for(int i = ;i<=n;i++) sum[i]+=dis[i];
for(int i = ;i<=n;i++)
{
dijkstra(i);
sum[i] += dis[x];
}
int max1 = ;
for(int i = ;i<=n;i++) if(sum[i]>max1) max1 = sum[i];
printf("%d\n",max1);
}
return ;
}
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。的更多相关文章
- 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 ...
- 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 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- 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、反向建图】
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 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- mac上Apache修改目录浏览权限
sudo vim /etc/apache2/httpd.conf <Directory "/Library/WebServer/Documents"> # # Poss ...
- JAVA设计模式初探之适配器模式(转)
1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...
- appium---adb通过wifi连接手机
前几天接到领导的安排,想要测试下apk的耗电量,可以通过手机adb命令进行监控手机电量的变化:但是这样如果通过USB连接手机的话,USB就会自动给手机进行充电,无法达到我们想要的结果,于是想到了通过w ...
- javaweb基础(30)_EL函数库
一.EL函数库介绍 由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用. 这些EL函数在JSTL开发包中进行描述,因此在JSP页 ...
- Drupal忘记管理员密码
第一步:登陆录到phpmyadmin(通用的mysql数据库管理工具),进入phpmyadmin后,找到与drupal7相关联数据库并在数据库中找到一张名为“users”的表,然后选择浏览. 第二步: ...
- java中利用JOptionPane类弹出消息框的部分例子
转: http://www.cnblogs.com/wangxiuheng/p/4449917.html http://blog.csdn.net/penjie0418/article/details ...
- iOS 中push和pop到底系统做了些什么事
iOS中的push和pop是一个很常用的视图切换方法,他们是成对出现的, 简而言之,push就是压栈,pop就是出栈! [self.navigationController pushViewContr ...
- CentOS7下Mysql5.7安装
下载并安装MySQL官方的 Yum Repository wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.no ...
- pycharm配置Git托管
利用Pycharm和github管理代码转载https://www.cnblogs.com/feixuelove1009/p/5955332.html git教程--廖雪峰git教程 转载https ...
- [译]The Python Tutorial#3. An Informal Introduction to Python
3. An Informal Introduction to Python 在以下示例中,输入和输出以提示符(>>>和...)的出现和消失来标注:如果想要重现示例,提示符出现时,必须 ...