题目

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. 杭电2034——人见人爱A-B

    #include <stdio.h> #include <algorithm> using namespace std; int main () { int a[110],b[ ...

  2. 基于 ArcGIS Silverlight API开发的WebGIS应用程序的部署

    部署流程概述 在微软的iis服务器上部署基于ArcGIS  Silverlight API的应用程序,主要包括以下几个步骤: 1)(可选)部署GIS服务 如果需要将GIS服务也部署在Web服务器上,则 ...

  3. C++ Vector 动态数组

    Vectors 包含着一系列连续存储的元素,其行为和数组类似.访问Vector中的任意元素或从末尾添加元素都可以在常量级时间复杂度内完成,而查找特定值的元素所处的位置或是在Vector中插入元素则是线 ...

  4. Nginx在嵌入式系统中的应用

    -----------------本文转载自 http://blog.csdn.net/xteda/article/details/39708009 ------------------------- ...

  5. 《通过脚本查看哪些ip被占用》shell笔记

    改脚本查看哪些ip被占用. #!/bin/bash for i in {1..10}   //赋予i变量1-10 do   //干什么 ping -c1 -w1 192.168.7.$i && ...

  6. 用 Function.apply() 的参数数组化来提高 JavaScript程序性能

    我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...

  7. 不再用.NET框架

    .NET 平台很棒.真的很棒.直到它不再那么棒.我为什么不再用 .NET?简单来说,它限制了我们选择的能力(对我来说很重要),转移了我们的注意力,使得我们向内认知它的安全性,替代了帮助我们认知外面广阔 ...

  8. Global::validateEmail

    /***************************************************************** (C) Copyright DENTSPLY Internatio ...

  9. Html.TextBoxFor三元判断

    @Html.TextBoxFor(item => item.DiscountOW,(Model.TripType == "单程" || (Model.TripType == ...

  10. jvm 数据区划分学习

    Java virtual machine 运行时数据存储区域划分 2015年1月25日 19:15 Pc  寄存器 Each Java Virtual Machine thread has its o ...