POJ 3268 最短路应用
题意很简单 正向图跑一遍SPFA 反向图再跑一边SPFA 找最大值即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int maxn=1000+1,maxm=100000+1,INF=1e+8;
int len[maxn][maxn],len1[maxn][maxn],dist[maxn],dist1[maxn];
vector<int> path[maxn],path1[maxn];
int n,m,source;
void SPFA(int dist[],int d[][maxn],vector<int>son[],int source)
{
bool inq[maxn];
int times[maxn];
memset(inq,0,sizeof(inq));
memset(times,0,sizeof(times));
for(int i=1;i<=n;i++)
dist[i]=INF;
dist[source]=0;
queue<int>q;
q.push(source);
times[source]++;
inq[source]=true;
while(!q.empty())
{
int now=q.front();
q.pop();
inq[now]=false;
for(int i=0;i<son[now].size();i++)
{
int np=son[now][i];
if(dist[np]>dist[now]+d[now][np])
{
dist[np]=dist[now]+d[now][np]; if(times[np]>n)return;
if(!inq[np]){times[np]++;q.push(np);}
}
}
}
} int main()
{freopen("t.txt","r",stdin);
scanf("%d%d%d",&n,&m,&source);
for(int i=0;i<m;i++)
{
int a,b,l;
scanf("%d%d%d",&a,&b,&l);
path[a].push_back(b);len[a][b]=l;
path1[b].push_back(a);len1[b][a]=l;
}
SPFA(dist,len,path,source);
SPFA(dist1,len1,path1,source);
int ans=0;
for(int i=1;i<=n;i++)
{
if((dist[i]+dist1[i])>ans)ans=dist[i]+dist1[i];
}
printf("%d\n",ans);
return 0;
}
POJ 3268 最短路应用的更多相关文章
- poj 3268 最短路dijkstra *
题目大意:给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最 ...
- 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算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- 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 (求给定两点之间的最短路)
http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...
- poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)
题目 Dijkstra,正反两次最短路,求两次和最大的. #define _CRT_SECURE_NO_WARNINGS //这是找出最短路加最短路中最长的来回程 //也就是正反两次最短路相加找最大的 ...
随机推荐
- ThinkPHP---thinkphp会话支持和文件载入
[一]会话控制 会话支持一般指cookie和session,在ThinkPHP里为了方便开发,封装了cookie和session方法. (1)session方法 在函数库封装了session方法 se ...
- 网络编程 - socket接收大数据
通过socket,实现客户端发送命令,将服务端执行出的结果,反回到客户端,主要4个步骤:1.服务端返回数据: 2.服务端返回数据的大小: 3.客户端接收返回数据的大小: 4.客户端按返回数据大小接收数 ...
- [转载] Linux Futex的设计与实现
Linux Futex的设计与实现 引子 在编译2.6内核的时候,你会在编译选项中看到[*] Enable futex support这一项,上网查,有的资料会告诉你"不选这个内核不一定能正 ...
- <SpringMvc>入门二 常用注解
1.@RequestMapping @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME ...
- Grid Convergence Index-- Post Processing in CFD
t Grid Convergence Index Table of Contents 1. Grid/mesh independence GCI 1.1. Richardson extrapola ...
- 洛谷 3979 BZOJ 3083 遥远的国度
[题解] 这道题除去根操作就是普通的树链剖分了.但是有换根操作怎么处理呢? 我们可以发现如果现在的根不在查询的点的子树里,那么对本次查询没有影响.如果现在的跟在查询的点x的子树里,那么答案将变为整棵树 ...
- 【Codeforces 339C】Xenia and Weights
[链接] 我是链接,点我呀:) [题意] 在天平上放砝码 你要在左边放一下然后到右边放一下 一直重复这样放m次 每次你放在其中一边都要让另外一边的重量比你少 你可以用1~10中的某些砝码 问你要怎样放 ...
- noip模拟赛 柜(暴力)
分析:暴力的方法是非常显然的,从起点走一次,从终点走一次,路径相交的点即为所求,但是这样存图都很难存下,而且如果数据极端可能要求R*C次,时间空间都受不了.如果不需要记录整张图,并且一次能移动很多步就 ...
- 解决CDH SparkStreaming任务启动之后executor不停增长的问题,num-executors配置不管用。
spark2-submit --class SparkKafka --master yarn --executor-memory 1G --num-executors 6 --driver-memor ...
- android在listview中放入从sdcard读取的bitmap
重写viewbinder public class viewbinder_bookmark implements SimpleAdapter.ViewBinder{ @Override public ...