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 ...
随机推荐
- SpringMvc入门五----文件上传
知识点: SpringMvc单文件上传 SpringMvc多文件上传 这里我直接演示多文件上传,单文件的上传就不说了,不过代码都是现成的. 效果预览: DEMO图: 添加文件上传j ...
- 南阳理工oj88--汉诺塔(一)
题目链接.http://acm.nyist.net/JudgeOnline/problem.php?pid=88 #include <stdio.h> /* //测试一下49999和500 ...
- Toad for Oracle 快捷键
F4 看表的结构 F5 执行对话框中的SQL,注意最后需要以;结尾 F7 清除当前编辑框中所有的sql F8 查看历史的sql语句 F9 执行当前行的sql F10 看菜单 Ctrl + F12 保存 ...
- C++ Maps 映射
C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空 ...
- extern 数组
最近比较关注C++对象的Linkage类型,然后今天突然想起extern数组这个奇葩的东西,稍微折腾了一下,顺手写个随笔. 首先在cpp中定义几个数组: ,,,,}; ,,,,}; ,,,,}; 然后 ...
- DEDECMS中,文章页直接输出字段名
文章页中,可直接输出字段名
- iframe整理学习笔记
朋友问了一个比较怪的问题,iframe下自适应的问题,因为很少使用iframe的原因,怀着对iframe的疑惑采用了一点点实践;以下frame表示针对的iframe元素 解决的方法:对iframe进行 ...
- EasyUI DataGrid分页数据绑定
记录东西感觉很痛苦,总结东西很痛苦,麻烦,不过为了下次的方便和知识的牢固以后要坚持总结. EasyUI DataGrid分页数据绑定 在解决方案中新建两个文件FormMain.aspx(html也可以 ...
- SharedPreferences的基本用法
获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法 2 调用Activity对象的getPreferences()方法 两 ...
- SpringUtil
/** SpringUtil.java {{IS_NOTE Purpose: Description: History: Thu Jun 1 13:53:53 2006, Created by hen ...