POJ1502
#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的更多相关文章
- POJ1502(最短路入门题)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7471 Accepted: 4550 Des ...
- POJ-1502 MPI Maelstrom 迪杰斯特拉+题解
POJ-1502 MPI Maelstrom 迪杰斯特拉+题解 题意 题意:信息传输,总共有n个传输机,先要从1号传输机向其余n-1个传输机传输数据,传输需要时间,给出一个严格的下三角(其实就是对角线 ...
- POJ-1502(基本dijikstra算法)
MPI Maelstrom POJ-1502 这题是求最短路,但是因为一开始看错题目,导致我去使用prime算法求最小生成树 题意是指一台机器发出信息后,还可以向其他的机器发送信息,所以不能使用pri ...
- MPI Maelstrom - POJ1502最短路
Time Limit: 1000MS Memory Limit: 10000K Description BIT has recently taken delivery of their new sup ...
- poj1502 spfa最短路
//Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...
- poj1502 最短路
题意:有n个处理器,给出n*n的邻接矩阵的一半,表示相互之间传输信息的花费(另一半与给出的一半对称,即双向都可传输),x表示不能传输,问从第一个处理器传输到所有处理器的最小花费总和是多少. 就是跑一遍 ...
- POJ1502: MPI Maelstrom
红果果的dijstra算法应用,这里采用邻接表存储图 小插曲:while(scanf("%d",&n))提交时内存超限,改成while(scanf("%d&quo ...
- POJ1502(Dijkstra)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5538 Accepted: 3451 题目链 ...
- MPI Maelstrom(East Central North America 1996)(poj1502)
MPI Maelstrom 总时间限制: 1000ms 内存限制: 65536kB 描述 BIT has recently taken delivery of their new supercom ...
随机推荐
- 离线安装maven
maven离线安装 1.在eclipse根目录下新建两个文件夹,links和myplugins,myplugins文件名可以自定义 2.下载maven http://pan.baidu.com/s/1 ...
- 代码审查 Code Review
为什么要做代码审查 代码审查最主要目的是保证软件质量,找出及修正在软件开发过程中的错误.同时,通过不同能力评审者对代码的分析和建议,可以很快提升编码能力和编码修养. 1. 保证软件质量 通常软件开发完 ...
- net Random 随机数重复的问题
在实际项目中不仅需要随机产生密码字符串,还要一次生成多个.我把生成随机字符串的方法放到for循环中,问题出现了. 生成的字符串,会重复. 经过多方查证,原因在代码. //使用与系统时间相关的种子 Ra ...
- TCP客户/服务器程序概述
一个回射服务器: 1)客户从标准输入读入一行文本,并写给服务器 2)服务器从网络输入读入这行文本,并回射给客户 3)客户从网络输入读入这行回射文本,并显示在标准输出上 回射输入行这样一个客户/服务器程 ...
- totolink的n200r路由在卓越网和京东网的价钱
totolink的n200r路由在卓越网和京东网的价钱, 应朋友需要帮忙买totolink的n200r的路由, 一向是在京东买电子产品的,之前都有在卓越网购物,所以今天也去看看卓越网上n200r的价格 ...
- 用C#实现网络爬虫(一)
网络爬虫在信息检索与处理中有很大的作用,是收集网络信息的重要工具. 接下来就介绍一下爬虫的简单实现. 爬虫的工作流程如下 爬虫自指定的URL地址开始下载网络资源,直到该地址和所有子地址的指定资源都下载 ...
- VitamioBundle-master
1. 下载资源 (1) 核心插件 VitamioBundle 下载地址:https://github.com/yixia/VitamioBundle (2) 官方示例 VitamioDemo 下载地址 ...
- logstash 通过mysql 慢日志了解(?m)
<pre name="code" class="html"># User@Host: zjzc_app[zjzc_app] @ [10.171.24 ...
- jquery validate 自定义验证方法 日期验证
jquery validate有很多验证规则,但是更多的时候,需要根据特定的情况进行自定义验证规则. 这里就来聊一聊jquery validate的自定义验证. jquery validate有一个方 ...
- (转载)apc_fetch
(转载)http://php.net/manual/zh/function.apc-fetch.php apc_fetch (PECL apc >= 3.0.0) apc_fetch — 从缓存 ...