题意:一些牛要去某一点參加聚会,然后再回到自己家,路是单向的,问花费时间最多的那头牛最少须要花费多长时间。

思路:从聚会地点返回,相当于是从某一点到其它各个点的最短路径。从牛的家中走到聚会地点,能够把路径反过来变成从聚会地点到各个点的最短路径,两个最短路径值加起来就是每头牛所花费的最小时间,找出最大的就可以。

我用了两个邻接表存路径,事实上这道题用邻接矩阵存更好做,矩阵横纵坐标翻转就把路径反转了,我用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+邻接表】的更多相关文章

  1. 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 ...

  2. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  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 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  6. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  9. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  10. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

随机推荐

  1. java实现文件传输

    在windows下装了个linux虚拟机,两者之间传输文件挺麻烦的.写了个简单的文件传输程序,来方便自己数据传送. server 端: import java.io.BufferedReader;im ...

  2. Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse

    1 Generators   Generator和list comprehension非常类似 Generators are a kind of iterator that are defined l ...

  3. 09-使用for循环输出空心菱形(循环)

    /** * 使用for循环输出空心菱形 * */ public class Test7 { public static void main(String[] args) { for (int i = ...

  4. 24篇HTTP博客

    http://www.cppblog.com/woaidongmao/category/11721.html

  5. codility上的问题(15) Xi 2012

    进入2012年的题 codility上的题目开始变难,变得有意思起来.给定两个长度在[1..300000]的只包含0和1的串S和T,它们是2进制表示的,S表示的数A不大于T表示的数B,即A<=B ...

  6. zTree实现地市县三级级联Action类

    zTree实现地市县三级级联Action类 ProvinceAction.java: /** * @Title:ProvinceAction.java * @Package:com.gwtjs.str ...

  7. Swift - 获取屏幕点击坐标下所有对象(SpriteKit游戏开发)

    对于场景内对象元件的点击响应,我们可以在场景的touchesBegan()方法中内统一处理. SKScene中touchesBegan()是响应屏幕点击的方法,在这里面我们可以先获取点击位置下所有的对 ...

  8. JSP的学习(6)——九大隐式对象及其out对象

    本篇将介绍JSP中的九大隐式对象,并重点介绍其中的out对象. 我们在之前的博客<JSP的学习(1)——基础知识与底层原理>一文中已经知道,JSP最终要被翻译和转换成Servlet,在转换 ...

  9. KMP poj

    题目来自:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2315188.html KMP算法开始是判断字符串b是否是字符串a的子串,朴素的算法是枚举 ...

  10. boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET

    boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET     boost::crc_32_type crc32;       crc32. ...