poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13611 | Accepted: 6138 |
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 题目及算法分析:
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#define N 1000+20
#define INF 0x3f3f3f3f using namespace std; int map[N][N];
int dis[N], ans[N];
bool vis[N];
int n, m, s; int Dijkstra(int s)
{
int i, j, k;
for(i=1; i<=n; i++)
dis[i]=map[s][i];
vis[s]=true;
for(k=1; k<n; k++)
{
int mi=INF, pos;
for(i=1; i<=n; i++)
{
if(vis[i]==false && dis[i]<mi )
{
mi=dis[i]; pos=i;
}
}
vis[pos]=true;
for(j=1; j<=n; j++)
{
if(vis[j]==false && dis[j]>dis[pos]+map[pos][j] )
dis[j]=dis[pos]+map[pos][j];
}
}
for(i=1; i<=n; i++)
{
ans[i]=ans[i]+dis[i];
}
return 0;
} void turn_over()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<i; j++)
swap(map[i][j], map[j][i] );
}
} int main()
{
scanf("%d %d %d", &n, &m, &s);
int u, v, w; for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(i==j) map[i][j]=0;
else map[i][j]=INF;
} for(int i=0; i<m; i++)
{
scanf("%d %d %d", &u, &v, &w);
map[u][v] = w;
}
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
Dijkstra(s);
turn_over();
memset(vis, false, sizeof(vis));
Dijkstra(s);
int cc=-1;
for(int i=1; i<=n; i++)
{
if(ans[i]>cc && ans[i]<INF )
cc=ans[i];
}
printf("%d\n", cc );
return 0;
}
poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )的更多相关文章
- POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)
题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently n ...
- 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 (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 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 (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- 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(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路
Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- 【前端阅读】——《编程之魂》摘记&读后感&思维导图
前言:这本书全名叫<编程之魂——与27为编程语言创始人对话>,它的内容以采访对话为主,以图通过和顶级大师的真实交流来调查:大师们为什么要创建某种编程语言,它的技术如何开发.如何教授和学习, ...
- java调试工具jdb
Finds and fixes bugs in Java platform programs. Synopsis jdb [options] [classname] [arguments] optio ...
- UITextField placeholder text color
iOS6 and Later 改变UITextField 中占位符 提示文本的文字颜色 在新版本中(iOS6以后)iOS提供一种 Key = value 属性的方式,来改变UI的属性内容.以UITex ...
- scrapy递归抓取网页数据
scrapy spider的parse方法能够返回两种值:BaseItem.或者Request.通过Request能够实现递归抓取. 假设要抓取的数据在当前页,能够直接解析返回item(代码中带**凝 ...
- Android应用开发:网络工具——Volley(一)
引言 网络一直是我个人的盲点,前一阵子抽空学习了一下Volley网络工具的用法,也透过源代码进行了进一步的学习,有一些心得想分享出来.在Android开发中,成熟的网络工具不少,Android自带了H ...
- 身份证归属地查询免费api接口代码
描写叙述 :依据身份证编号 查询归属地信息. 身份证实体类: package org.wx.xhelper.model; /** * 身份证实体类 * @author wangxw * @versio ...
- Unity3D研究院编辑器之脚本设置ToolBar及脚本设置顶视图
Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...
- android假设给TextView或EditText的email链接加下划线,并在点击在email连接上能够弹框显示
怎样把textview的一些文字加上背景色: Spannable str = new SpannableString("#fdsfdfsdfdsfd#"); Matcher mat ...
- IOS开发之----异常处理
本文转载至 http://blog.csdn.net/chenyong05314/article/details/7906593 转载自:http://blog.sina.com.cn/s/blog_ ...
- python 基础 9.0 安装MySQL-python-1.2.5客户端
一. 安装客户端 python 标准数据库接口为Python DB-API,Python DB-API 为开发人员提供了数据应用编程接口.参考地址:https://wiki.python.or ...