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 ...
随机推荐
- Jquery Mobile学习
<!doctype html> <html lang="zh-hans"> <head> <meta charset="UTF- ...
- php处理中文字符串
使用mbstring 先转换成UTF-8编码 mb_convert_encoding(Input::get('tags'),'UTF-8') mbstring用法参考http://php.net/ma ...
- CSS样式margin:0 auto不居中
<style type="text/css">html,body{height:100%;width:960px;}.container{background-colo ...
- jquery 实现文本框scroll上下动
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- python日期时间处理
time模块 #-*- coding: utf-8 -*- """ #获取当前时间的时间戳(单位秒) time.time() #推迟指定秒数后再运行 time.sleep ...
- 总结:ARM逻辑和高级C(朱老师物联网学习)
开始学习朱老师物联网的视频是国庆节的那几天开始的,刚刚开始的时候是想自己在网上找一些嵌入式方面的视频资料,也找了很多的资料臂如“国嵌视频”“达内的视频”,之后也化了十几块钱在淘宝上面买了几十个G的视频 ...
- ORACLE数据库操作基本语句
1.登陆SPL*PLUS [username/password] [@server] as [sysdba|sysoper] eg. system/password or connect sys/pa ...
- IIS 10 安装URLRewrite组件 方式
1.Open Regedit > HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp 2.Edit "MajorVersion" an ...
- poj 2892 &&hdu 1540 Tunnel Warfare
http://poj.org/problem?id=2892 #include <cstdio> #include <cstring> #include <algorit ...
- 【递归】Vijos P1132 求二叉树的先序序列(NOIP2001普及组第三题)
题目链接: https://vijos.org/p/1132 题目大意: 给定二叉树的中序和后序遍历,求该二叉树先序遍历. 题目思路: [递归] 这题妥妥递归. 二叉树先序根左右,中序左根右,后序左右 ...