POJ--3268--Silver Cow Party【SPFA+邻接表】
题意:一些牛要去某一点參加聚会,然后再回到自己家,路是单向的,问花费时间最多的那头牛最少须要花费多长时间。
思路:从聚会地点返回,相当于是从某一点到其它各个点的最短路径。从牛的家中走到聚会地点,能够把路径反过来变成从聚会地点到各个点的最短路径,两个最短路径值加起来就是每头牛所花费的最小时间,找出最大的就可以。
我用了两个邻接表存路径,事实上这道题用邻接矩阵存更好做,矩阵横纵坐标翻转就把路径反转了,我用SPFA写想练练手,一直都不会手写SPFA,做几道题找找感觉。
AC竟然用时0MS。。
#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 100100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct node{
int u,v,next;
}edge[MAXN],redge[MAXN];
int head[MAXN],rhead[MAXN],dist[MAXN],vis[MAXN],ans[MAXN];
int cnt,rcnt,n,m,x;
void add_edge(int a,int b,int c){
edge[cnt].u = b;
edge[cnt].v = c;
edge[cnt].next = head[a];
head[a] = cnt++;
redge[rcnt].u = a;
redge[rcnt].v = c;
redge[rcnt].next = rhead[b];
rhead[b] = rcnt++;
}
void spfa(int type){
int i,j;
for(i=1;i<=n;i++){
dist[i] = INF;
}
dist[x] = 0;
memset(vis,0,sizeof(vis));
vis[x] = 1;
queue<int>q;
q.push(x);
while(!q.empty()){
int temp = q.front();
q.pop();
vis[temp] = 0;
for(i=type?rhead[temp]:head[temp];i!=-1;i=type?redge[i].next:edge[i].next){
int lu = type?redge[i].v:edge[i].v;
if(lu+dist[temp]<dist[type?redge[i].u:edge[i].u]){
dist[type?redge[i].u:edge[i].u] = lu + dist[temp];
if(!vis[type?redge[i].u:edge[i].u]){
vis[type?redge[i].u:edge[i].u] = 1;
q.push(type?redge[i].u:edge[i].u);
}
}
}
}
}
int main(){
int i,j,a,b,c;
scanf("%d%d%d",&n,&m,&x);
memset(head,-1,sizeof(head));
memset(rhead,-1,sizeof(rhead));
cnt = rcnt = 0;
int maxm = 0;
for(i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
spfa(0);
for(i=1;i<=n;i++){
if(i!=x) ans[i] = dist[i];
}
spfa(1);
for(i=1;i<=n;i++){
if(i!=x) ans[i] += dist[i];
if(ans[i]>maxm) maxm = ans[i];
}
printf("%d\n",maxm);
return 0;
}
POJ--3268--Silver Cow Party【SPFA+邻接表】的更多相关文章
- 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 (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 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 最短路
原题链接: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 ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
随机推荐
- boost中asio网络库多线程并发处理实现,以及asio在多线程模型中线程的调度情况和线程安全。
1.实现多线程方法: 其实就是多个线程同时调用io_service::run for (int i = 0; i != m_nThreads; ++i) { boo ...
- Thinking in UML 学习笔记(一)——建立对象模型
一.面向对象的本质 面向对象的本质是抽象,当系统达到了超越其处理能力的程度,我们能够抽象出我们能够处理的范围来提成抽象级别,这样就能够构建更大.更复杂的系统. 现实世界和对象世界之间存在着一道沟壑,这 ...
- fzu 1913 Easy Comparison(字符串)
题目链接:fzu 1913 Easy Comparison 题目大意:给出一个字符串,计算与它按照字典序排序排列后的字符串有多少个位置不同. 解题思路:水体,sort一下,然后遍历一遍就好. #inc ...
- 测试 __try, __finally, __except(被__finally捕获的异常, 还会被上一级的__except捕获。反之不行)
C语言标准是没有 try-catch语法 的, M$家自己提供了一组. /// @file ClassroomExamples.c /// @brief 验证C语言的非标准try, catch #in ...
- [置顶] 提高生产力:Web开发基础平台WebCommon的设计和实现
Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...
- Android开发:在onTouchEvent中处理任意时间的长按事件
Android提供了GestureDetector类来处理一些常用的手势操作,比如说 onLongPress,onFling 等.但这里不使用GestureDetector,而是直接在自定义View重 ...
- 论文阅读笔记 - Mesos: A Platform for Fine-Grained ResourceSharing in the Data Center
作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 更多论文阅读笔记 http:/ ...
- 如何使用Reaver破解Wi-Fi网络的WPA密码
via: http://lifehacker.com/5873407/how-to-crack-a-wi+fi-networks-wpa-password-with-reaver 译者:Mr小眼儿 本 ...
- Java字符串找出4个字节长度的字符
不解释,直接上代码: 由于Iteye代码贴四个字节的UTF-8字符出错,特能图的方式发布几个特殊字符: public class Byte4Check { public static void m ...
- JavaScript快速入门(六)——DOM
概念扫盲 DOM DOM是 Document Object Model(文档对象模型)的缩写,是W3C(万维网联盟)的标准.DOM 定义了访问 HTML 和 XML 文档的标准:“W3C 文档对象模型 ...