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 ...
随机推荐
- Linux命令学习计划【sed】
引言: Sed命令是linux里用于文本行处理的命令. 为了便于说明,我在/usr/dict下创建了字典words并以此作为演示模板 先用nl 打印下words内容: *打印篇: Q1:如何打印某一行 ...
- JavaScript-学习一字符串
字符串可以存储一系列字符,如 "John Doe". 字符串可以是插入到引号中的任何字符.你可以使用单引号或双引号: 用于字符串的 + 运算符 + 运算符用于把文本值或字符串变量加 ...
- OpenMP 并行化处理测试
OpenMP 并行化处理测试 #pragma omp parallel for 这条语句是用来指定后面的for循环语句变成并行执行的,将for循环里的语句变成并行执行后效率会不会提高呢?还是测试一 下 ...
- ATR的基本结构与意义(无历史字符部分)
Reset 3B FA 13 00 00 81 31 FE 45 4A 43 4F 50 34 31 56 32 32 31 96 复位应答 ATR TS( The Initial character ...
- 【HDOJ】4956 Poor Hanamichi
基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. #include <cstdio> int f(__int64 x) { int i, sum; i = ...
- BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节
1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 606 Solved: 289 ...
- POJ 1734 Sightseeing trip
题目大意: 求一个最小环. 用Floyd 求最小环算法. #include <iostream> #include <cstdlib> #include <cstdio& ...
- Hive学习笔记【转载】
本文转载自:http://blog.csdn.net/haojun186/article/details/7977565 1. HIVE结构 Hive 是建立在 Hadoop 上的数据仓库基础构架. ...
- Selenium IDE整理
安装 Step1: 下载Firefox浏览器 http://www.firefox.com.cn/ Step2: 安装Selenium IDE插件 http://seleniumhq.org/down ...
- SuppressWarnings的警告
简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...