#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;
const int N = 105;
const int MAX = 0xfffffff;
int edge[N][N];
int n, e;
int s[N];
bool vis[N];
void init()
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
edge[i][j] = -1;
}
}
for (int i = 0; i < n; ++i)
{
vis[i] = false;
s[i] = MAX;
}
} void dijkstra()
{
int p, te, tm;
s[0] = 0;
vis[0] = true;
p = 0;
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n; ++j)
{
if (!vis[j] && edge[p][j] != -1 && s[p] + edge[p][j] < s[j])
{
s[j] = s[p] +edge[p][j];
}
}
tm = MAX;
for (int j = 0; j < n; ++j)
{
if (!vis[j] && s[j] < tm)
{
tm = s[j];
te = j;
}
}
vis[te] = true;
p = te;
}
} int change(char str[])
{
if (str[0] == 'x') return -1;
int ret = 0;
while (*str)
{
ret = ret * 10 + *str++ - '0';
}
return ret;
} int main()
{
char str[35];
int dis;
scanf("%d", &n);
init();
for (int i = 0; i < n; ++i)
{
edge[i][i] = 0;
for (int j = 0; j < i; ++j)
{
scanf("%s", str);
dis = change(str);
//cout<<*str<<" ";
edge[i][j] = edge[j][i] = dis;
}
}
dijkstra();
int MAX = 0;
for (int i = 1; i < n; ++i)
{
if (s[i] > MAX) MAX = s[i];
}
printf("%d\n", MAX);
return 0;
}

POJ1502

题意:求单源最短路径,不过中间需要处理下特殊字符的情况,输入是个坑





输入:

n(点集)

n-1行

邻接矩阵下三角,x字符另外处理





输出:

源点到所有点最短路径中最大的值





解题思路:

单源最短路径有很多算法可以求解,简单一点的就是dij(N^2),另外可以用bellman-ford算法求,相应SPFA是对bellman-ford队列的优化,具体优化操作则是在存取队列节点时,不断进行松弛操作,直至队列为空。





贴上算法与实现书上用SPFA实现的代码以及floyd算法

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <stdlib.h>
#define maxn 1000
#define INF 100000000
using namespace std;
vector < pair <int,int> > g[maxn+10];
char ch[10000],ans[100];
int node,edge,src;
int dis[maxn+10];
bool inQueue[maxn+10];
queue<int> que;
pair <int,int> suit;
void SPFA()
{
for (int i=0; i<=node; i++)
dis[i]=INF;
memset(inQueue,false,sizeof(int)*(node+5));
dis[src]=0;
while(!que.empty())
{
que.pop();
}
que.push(src);
inQueue[src]=true;
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=0; i!=g[u].size(); i++)
{
if ( dis[u] + g[u][i].second <dis[g[u][i].first] )
{
dis[g[u][i].first] = dis[u] + g[u][i].second;
if (!inQueue[g[u][i].first])
{
inQueue[g[u][i].first] = true;
que.push(g[u][i].first);
}
}
}
inQueue[u]=false;
}
}
int main()
{
while( scanf("%d",&node)!=EOF )
{
getchar();
for(int i=2; i<=node; i++)
{
gets(ch);
int add,ji=1;
char *p=ch;
while(sscanf(p,"%s%n",ans,&add)!=EOF)
{
if(ans[0]=='x')
{
ji++;
p=p+add;
continue;
}
suit.second=atoi(ans);
suit.first=ji;
g[i].push_back(suit);
suit.first=i;
g[ji].push_back(suit);
p=p+add;
ji++;
}
}
src=1;
SPFA();
int max=-1;
for(int i=1; i<=node; i++)
{
if( max < dis[i] )
max=dis[i];
}
printf("%d\n",max);
}
return 0;
}
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}

POJ1502的更多相关文章

  1. POJ1502(最短路入门题)

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7471   Accepted: 4550 Des ...

  2. POJ-1502 MPI Maelstrom 迪杰斯特拉+题解

    POJ-1502 MPI Maelstrom 迪杰斯特拉+题解 题意 题意:信息传输,总共有n个传输机,先要从1号传输机向其余n-1个传输机传输数据,传输需要时间,给出一个严格的下三角(其实就是对角线 ...

  3. POJ-1502(基本dijikstra算法)

    MPI Maelstrom POJ-1502 这题是求最短路,但是因为一开始看错题目,导致我去使用prime算法求最小生成树 题意是指一台机器发出信息后,还可以向其他的机器发送信息,所以不能使用pri ...

  4. MPI Maelstrom - POJ1502最短路

    Time Limit: 1000MS Memory Limit: 10000K Description BIT has recently taken delivery of their new sup ...

  5. poj1502 spfa最短路

    //Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...

  6. poj1502 最短路

    题意:有n个处理器,给出n*n的邻接矩阵的一半,表示相互之间传输信息的花费(另一半与给出的一半对称,即双向都可传输),x表示不能传输,问从第一个处理器传输到所有处理器的最小花费总和是多少. 就是跑一遍 ...

  7. POJ1502: MPI Maelstrom

    红果果的dijstra算法应用,这里采用邻接表存储图 小插曲:while(scanf("%d",&n))提交时内存超限,改成while(scanf("%d&quo ...

  8. POJ1502(Dijkstra)

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5538   Accepted: 3451 题目链 ...

  9. MPI Maelstrom(East Central North America 1996)(poj1502)

    MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercom ...

随机推荐

  1. FLEX 网格布局及响应式处理

    上一篇文章用Flex实现BorderLayout,这一章我们来实现常用的网格布局和响应式处理. 首先我们定义HTML结构,主Box为grid,每项为grid-cell,下面就是我们HTML代码结构. ...

  2. [jQuery] $.grep使用

    1.$.grep的功能是查找过滤功能的数组,原数组不受影响. 2.参数定义 jQuery.grep( array, function(elementOfArray, indexInArray), [ ...

  3. 关于$GLOBALS['ecs']->table()的问题?

    $ecs对象定义数据库和表前缀 class ECS { var $db_name = ''; var $prefix = 'ecs_'; function ECS($db_name, $prefix) ...

  4. php 数组 类对象 值传递 引用传递 区别

    一般的数据类型(int, float, bool)不做这方面的解说了 这里详细介绍一下数组和的类的对象作为参数进行值传递的区别 数组值传递 实例代码: <?php function main() ...

  5. 大脑皮层是如何工作的 《人工智能的未来》(<On intelligence>)读书笔记

    PS:今年寒假的读书笔记,挖下的坑已无力再填...不过有关智能和人工智能的书还是要继续读的~ 正文: 我觉得书名翻译不是很确切,全书讨论的核心应该更是在“真”智能:讨论对人脑智能的理解,可以怎样帮助我 ...

  6. sql server 调优----索引缺失

    SELECT mig.index_group_handle, mid.index_handle, CONVERT (decimal (28,1), migs.avg_total_user_cost * ...

  7. sqlserver 进行MD5加密

    官方定义函数: HashBytes ( '<algorithm>', { @input | 'input' } )  <algorithm>::= MD2 | MD4 | MD ...

  8. CentOS 6.4 64位 源码编译hadoop 2.2.0

    搭建环境:Centos 6.4 64bit 1.安装JDK 参考这里2.安装mavenmaven官方下载地址,可以选择源码编码安装,这里就直接下载编译好的wget http://mirror.bit. ...

  9. 转:Java开发牛人十大必备网站

    原文来自于:http://www.importnew.com/7980.html 以下是我收集的Java开发牛人必备的网站.这些网站可以提供信息,以及一些很棒的讲座, 还能解答一般问题.面试问题等.质 ...

  10. 解决octave for windows安装包无法通过SourceForge下载的问题

    近期SourceForge访问不了,可以通过访问SourceForge的ftp镜像ftp://sourceforge.nchc.org.tw/进行下载: ftp下载工具可以使用FileZilla,可在 ...