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 //这是找出最短路加最短路中最长的来回程 //也就是正反两次最短路相加找最大的 ...
随机推荐
- Linux修改系统时间与时区
GMT (Greewich Mean Time) 格林威治标准时间:GMT是老的时间计量标准,根据地球的自转和公转来计算时间,也就是太阳每天经过位于英国伦敦郊区的皇家格林尼治天文台的标准时间就是中午 ...
- ubutun 创建左面快捷方式
#http://blog.csdn.net/jizi7618937/article/details/51012552
- on绑定事件支持的事件类型
blurfocusfocusinfocusoutloadresizescrollunloadclickdblclickmousedownmouseupmousemovemouseovermouseou ...
- 3. Python中的分支判断、循环
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的分支判断.循环 1. 分支条件判断 1.1 比较操作 以下是数之间常见的比较操作,例如5>3就是数学意义上的比较,5是大 ...
- PAT 1143 Lowest Common Ancestor
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 哈希表模板(Hash set)
省选前最后的复(chui si)习(zheng zha). 上模板吧 namespace Hash_Table{ #define inf ~0U>>1 #define MaxN 10010 ...
- JUnit基本用法
JUnit的一些注意事项: 测试方法必须使用@Test修饰 测试方法必须使用public void进行修饰,不能带参数 一般使用单元测试会新建一个test目录存放测试代码,在生产部署的时候只需要将te ...
- Spring核心技术(一)——IoC容器和Bean简介
IoC容器和Bean简介 这章包括了Spring框架对于IoC规则的实现.Ioc也同DI(依赖注入).而对象是通过构造函数,工厂方法,或者一些Set方法来定义对象之间的依赖的.容器在创建这些Bean对 ...
- 最大公约数GCD
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 输入2个正整数A,B,求A与B的最大公约数. Input 2个数A,B,中间用空格隔开.(1<= A,B <= ...
- Self Centos + Windows server 2016
Set up by Derek: 2019-1-25 登陆个人物理机: license 60天Free , 如果过期,就在 VMware ESXI 6.5.0的黑屏界面去reset. https:/ ...