题目

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(最短路,正反两次,这个模版好)的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  2. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  3. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  4. 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 ...

  5. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  6. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

  9. 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 ...

随机推荐

  1. poj 3669 Meteor Shower

                                                                                                      Me ...

  2. 【风马一族_Android】适合你 --- 大概的描述

    适合你:专注于解决毕业生,离校所遗留的闲置教材的去向问题的一款APP. 目前的现状:毕业生的闲置教材,被清理宿舍的阿姨.大叔所清理到垃圾场,或拿到收破烂的地方,卖掉. 在毕业季中,存在的闲置物品不只有 ...

  3. 解析php开发中的中文编码问题

    其实php开发中的中文编码并没有想像的那么复杂,虽然定位和解决问题没有定规,各种运行环境也各不尽然,但后面的原理是一样的. 了解字符集的知识是解决字符问题的基础. PHP程序设计中中文编码问题曾经困扰 ...

  4. Cookie 的运行机制以及常用规则

    一   setCookie        bool setcookie ( string name [, string value [, int expire [, string path [, st ...

  5. 使用Sass优雅并高效的实现CSS中的垂直水平居中(附带Flex布局,CSS3+SASS完美版)

    实现css水平垂直居中的方法有很多,在这里我简单的说下四种比较常用的方法: 1.使用CSS3中的Flex布局 对于flex,我们要了解的是它是一个display的属性,而且必须要给他的父元素设置fle ...

  6. WPF学习01:初始XAML浅析

    本文内容: 浅析WPF应用默认创建的XAML中元素.attributes. 新建WPF工程“HelloWPF”. 初始创建的主窗体XAML代码如下: <Window x:Class=" ...

  7. java项目中读取src目录下的文件

    private void getUser(String tmpfile){ Properties props = new Properties(); props.load(DbTask.class.g ...

  8. MYSQL创建多张表,相同表结构,不同表名

    #!/bin/bashfor i in {0..63}domysql -u$1 -p$2 -h127.0.0.1 <<EOFuse yoon;create table ivc_pre_de ...

  9. UnitTest

    using Bll; using Model; using Dal; using NUnit.Framework; using NUnit.Mocks; using System.ServiceMod ...

  10. 虚拟机Linux下找不到/dev/cdrom

    问题描述: 笔者欲更新一下VMwareTools,结果发现虚拟机Linux上找不到设备"/dev/cdrom",当然也就不能通过命令"mount /dev/cdrom / ...