poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)
Dijkstra,正反两次最短路,求两次和最大的。
#define _CRT_SECURE_NO_WARNINGS
//这是找出最短路加最短路中最长的来回程
//也就是正反两次最短路相加找最大的和
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; const int MAXN=; #define typec int
const typec INF=0x3f3f3f3f;//防止后面溢出,这个不能太大
bool vis[MAXN];
typec cost1[MAXN][MAXN],cost2[MAXN][MAXN];
typec lowcost1[MAXN],lowcost2[MAXN];
void Dijkstra1(int n,int beg)
{
for(int i=;i<=n;i++)
{
lowcost1[i]=cost1[beg][i];vis[i]=false;
}
for(int i=;i<=n;i++)
{
typec temp=INF;
int k=-;
for(int j=;j<=n;j++)
{
if(!vis[j]&&lowcost1[j]<temp)
{
temp=lowcost1[j];
k=j;
}
}
vis[k]=true;
for(int l=;l<=n;l++)
{
if(!vis[l])
{
lowcost1[l]=min(lowcost1[l],lowcost1[k]+cost1[k][l]);
}
}
}
}
void Dijkstra2(int n,int beg)
{
for(int i=;i<=n;i++)
{
lowcost2[i]=cost2[beg][i];vis[i]=false;
}
for(int i=;i<=n;i++)
{
typec temp=INF;
int k=-;
for(int j=;j<=n;j++)
{
if(!vis[j]&&lowcost2[j]<temp)
{
temp=lowcost2[j];
k=j;
}
}
vis[k]=true;
for(int l=;l<=n;l++)
{
if(!vis[l])
{
lowcost2[l]=min(lowcost2[l],lowcost2[k]+cost2[k][l]);
}
}
}
} int main()
{
int n,m,t,i,j,a,b,c;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
for(i=;i<=n;i++)
for(j=;j<=n;j++)
cost1[i][j]=cost2[i][j]=(i==j)? :INF; for(i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(cost1[a][b]>c)
{
cost1[a][b]=c;//正
}
if(cost2[b][a]>c)
{
cost2[b][a]=c;//反
}
}
Dijkstra1(n,t);//正
Dijkstra2(n,t);//反
int ans=-;
for(i=;i<=n;i++)
{
lowcost1[i]+=lowcost2[i];
ans=ans>lowcost1[i]? ans:lowcost1[i];
}
printf("%d\n",ans);
} return ;
}
poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)的更多相关文章
- 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 (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- poj 3268 Silver Cow Party(最短路dijkstra)
描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...
- 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)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- 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(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- 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 ...
随机推荐
- 洛谷 P3399 丝绸之路
题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲,将那里的香 ...
- HDU 1165 Eddy's research II
题意:已知,求A(m, n). 分析:根据样例模拟一下过程就可以找出递推关系. #include<cstdio> #include<cstring> #include<c ...
- Windows Phone 8仿Toast弹出通知。
Wp8后台收到消息可以弹出Toast通知,可是应用在前台跑的时候却好像没有办法弹出Toast通知.而需求上有要求要弹出Toast通知,在这种情况下,就开始考虑是否能够做一个类似Toast的弹出通知呢? ...
- jquery的change 事件
jquery的change 事件 . <script src="jquery.min.js" type="text/javascript">< ...
- ExtJS实战 01——HelloWorld
前言 Extjs5的发布已经有些日子了,目前的最新稳定版本是Extjs5.1.0,我们可以在官方网站进行下载.不过笔者今天访问得到的是502Bad Gateway,原因可能是sencha的nigix没 ...
- 例题6-4 Broken Keyboard UVa11988
题目分析: 起初这道题目没有做出来,原因是我一直想把整块区域一并插入,而不是逐个插入.今后做题应该注意这个问题,把问题分解去考虑,也许会少走许多弯路. 下边附上AC代码 #include <cs ...
- 正则PerlRegEx实现的批量替换指定文件中的标签
示例: 一个朋友需要而编写的标签升级更新. 速度超快,1w个文件大概4,5秒,本想加个多线程显示进度,后来想想算了 主要代码: reg.RegEx := '<' + Edit_regular1. ...
- 【译】Android系统简介
简介 本文主要介绍Android的基础知识和体系结构,本文主题: 简介什么是Android,为什么开发者需要关注Android: Android体系结构(如Linux Kernel, Librari ...
- 1066. Root of AVL Tree (25)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- iOS多线程编程Part 2/3 - NSOperation
多线程编程Part 1介绍了NSThread以及NSRunLoop,这篇Blog介绍另一种并发编程技术:NSOPeration. NSOperation & NSOperationQueue ...