图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12674 | Accepted: 5651 |
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
Source
Mean:
原意:草场上有n个农场,农场之间有一些路径,每个农场里住着一头牛,现在x农场的牛要过生日开party,其他农场的牛要到该农场去参加party,现在让你选择一头来回耗时最多的一头牛出来,输出时间。
给你一个n个结点、m条边的有向图,现在要你求从n-1个结点到达指定的一个结点的来回最长路。
analyse:
这题思路很巧妙,我们在存图的时候用链式向前星来存,存的时候就建两次边,一次正向,一次反向,用一个flag来标记一下。然后用两遍spfa,第一遍求出从x点出发的正向图到每个结点的最短路,第二遍求出从x点出发的反向图到每个结点的最短路,最后将两次的最短路对应相加,求出最大值即为最终的answer。
Time complexity:O(n*k)
Source code:
//Memory Time
// 3521K 241MS
//by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAXV 1010
#define MAXE 100010
#define LL long long
using namespace std;
namespace Adj
{
struct Node
{
int to,next,val;
bool flag;
} edge[MAXE<<1];
int top,head[MAXV];
void init()
{
top=1;
memset(head,0,sizeof(head));
}
void addEdge(int u,int v,int val)
{
edge[top].to=v;
edge[top].val=val;
edge[top].flag=1;
edge[top].next=head[u];
head[u]=top++; edge[top].to=u;
edge[top].val=val;
edge[top].flag=0;
edge[top].next=head[v];
head[v]=top++;
}
}
using namespace Adj;
int n,m,x,ans;
bool vis[MAXV];
int dis[MAXV];
int dis1[MAXV];
void spfa(bool flag)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++)
dis[i]=INT_MAX;
queue<int>Q;
Q.push(x);
vis[x]=1;
dis[x]=0;
while(!Q.empty())
{
int now=Q.front();
Q.pop();
vis[now]=0;
for(int i=head[now];i;i=edge[i].next)
{
if(flag==1)
{
if(edge[i].flag==0)
continue;
}
else
{
if(edge[i].flag==1)
continue;
}
int son=edge[i].to;
int val=edge[i].val;
if(dis[now]+val<dis[son])
{
dis[son]=dis[now]+val;
if(!vis[son])
{
vis[son]=1;
Q.push(son);
}
}
}
}
if(flag==1)
{
for(int i=1;i<=n;i++)
dis1[i]=dis[i];
}
else
{
int Max=INT_MIN;
for(int i=1;i<=n;i++)
{
dis[i]+=dis1[i];
if(dis[i]>Max)
Max=dis[i];
}
ans=Max;
}
} int main()
{
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
while(~scanf("%d %d %d",&n,&m,&x))
{
init();
int u,v,w;
while(m--)
{
scanf("%d %d %d",&u,&v,&w);
addEdge(u,v,w);
}
spfa(1);
spfa(0);
printf("%d\n",ans);
}
return 0;
}
图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party的更多相关文章
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
- 图论 --- spfa + 链式向前星 (模板题) dlut 1218 : 奇奇与变形金刚
1218: 奇奇与变形金刚 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 130 Solved: 37[Submit][Status][Web Boa ...
- Tarjan模版(链式向前星表示方法)
这道模版用到了链式向前星表示法: struct node { int v,next; }edge[]; void add(int x,int y) { edge[++cnt].next=heads[x ...
- 【数据结构】链式向前星知识点&代码
代码: struct NODE{ int to; int nxt; int c; }node[MM];//链式向前星 ; void add(int a,int b,int c){ node[lcnt] ...
- 【bfs+链式向前星】防御僵尸(defend)计蒜客 - 45288
题目: A 国有 n 座城市,n−1 条双向道路将这些城市连接了起来,任何两个城市都可以通过道路互通. 某日,A 国爆发了丧尸危机,所有的幸存者现在都聚集到了 A 国的首都(首都是编号为 1 的城市) ...
- 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 ...
随机推荐
- 【C语言学习】《C Primer Plus》第9章 函数
学习总结 1.函数有利于我们可以省去重复的代码,函数可以使程序更加模块化,从而有利于程序的阅读.修改和完善.我们在系统设计或架构设计的时候,往往追求的是模块化.组件化.松耦合,而函数就是其代码的表现. ...
- 赴美工作常识(Part 3 - 英语)
在<Part 2 - 申请>的评论中有人问英语要达到何种水平,以及如何提高.其实英语也不是我的强项,只是刚刚好做到能够沟通而已.由于我在知乎上回到过一个类似问题,我就基于那个答案简单说一下 ...
- [自己动手玩黑科技] 1、小黑科技——如何将普通的家电改造成可以与手机App联动的“智能硬件”
NOW, 步 将此黑科技传授予你~ 一.普通家电控制电路板分析 普通家电,其人机接口一般由按键和指示灯组成(高端的会稍微复杂,这里不考虑) 这样交互过程,其实就是:由当前指示灯信息,按照操作流程按相应 ...
- http学习笔记(二)—— 嘿!伙计,你在哪?(URL)
我们之所以希望浏览网页,其中一个重要的原因就是庞大的web世界中有很丰富的资源,他就像哆啦a梦的口袋,随时都能拿出我们想要的宝贝.这些资源通过http被传送到我们的浏览器,并展示到我们的屏幕上.而我们 ...
- Atitit Gaussian Blur 高斯模糊 的原理and实现and 用途
Atitit Gaussian Blur 高斯模糊 的原理and实现and 用途 1.1. 高斯模糊 的原理(周边像素的平均值+正态分布的权重1 1.2. 高斯模糊 的用途(磨皮,毛玻璃效果,背景虚化 ...
- Atitit 破解qq空间(2)-------探测权限
Atitit 破解qq空间(2)-------探测权限 /AtiPlatf_cms/src/com/attilax/net/httpTest.java package com.attilax.net ...
- fir.im Weekly - iOS开发中的Git流程
本期 fir.im Weekly 收集了微博上的热转资源,包含 Android.iOS 开发工具.源码等好用的轮子,还有一些 APP 设计的 Tips,希望对你有用. 精仿知乎日报 iOS 端 @我偏 ...
- js 的使用原则
1.平稳退化:确保页面在没有javaScript的情况下也能正常运行 2.向后兼容:确保老版本的浏览器不会因为javaScript而死掉 3.性能最优:确保脚本执行的性能最优 4.页面与js分离:最大 ...
- StringUtils 的用法
1.public static boolean isEmpty(String str) 判断某字符串是否为empty,标准是 null == str 或 str.length() == 0 2.pub ...
- 利用跨域请求来隐藏firbug控制台中的Ajax请求
本文链接:http://www.orlion.ml/63/ 普通jquery的Ajax请求在控制台中是可见的,如下: 而在利用jsonp(json with padding)进行Ajax跨域时,发现A ...