poj2263 zoj1952 Heavy Cargo(floyd||spfa)
这道题数据范围小,方法比较多。我用floyd和spfa分别写了一下,spfa明显有时间优势。
一个小技巧在于:把城市名称对应到数字序号,处理是用数字。
方法一:spfa
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
int n,r,cas=,ton,num,used[maxn],d[maxn],m[maxn][maxn];
char city[maxn][];
char s[],e[];
vector<int>v[maxn];
int index(char *ss)
{
int i;
for(i=;i<num;i++)
{
if(strcmp(ss,city[i])==)
{
return i;
}
}
strcpy(city[num++],ss);
return i;
}
void ini()
{
num=;
for(int i=;i<n;i++)
{
strcpy(city[i],"\0");
v[i].clear();
used[i]=;
d[i]=INF;
}
}
int spfa(int s,int e)
{
queue<int>q;
q.push(s);
used[s]=;
while(!q.empty())
{
int t=q.front();
used[t]=;
q.pop();
int siz=v[t].size();
for(int i=;i<siz;i++)
{
int k=v[t][i];
/*if(used[k]==0) {q.push(k);used[k]=1;}
if(d[k]<INF) d[k]=max(d[k],min(d[t],m[t][k]));
else d[k]=min(d[t],m[t][k]);*/
/*上面注释掉的这种写法不对,一旦有环就会陷入死循环(即使这题不会有
负边)。还是对spfa理解的不够。需要更新的点才要考虑入队的问题,不需要
更新的点肯定不入队,最后所有点都不用再更新了队列为空退出循环。而不是
遇到一个点就入队。*/
if(d[k]==INF)
{
d[k]=min(d[t],m[t][k]);
q.push(k);used[k]=;
}
else if(min(d[t],m[t][k])>d[k])
{
d[k]=min(d[t],m[t][k]);
if(used[k]==) {q.push(k);used[k]=;}
}
}
}
return d[e];
}
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&r)==)
{
if(n==&&r==) break;
printf("Scenario #%d\n",++cas);
ini();
for(int i=;i<r;i++)
{
scanf("%s%s%d",s,e,&ton);
int t1=index(s);
int t2=index(e);
v[t1].push_back(t2);
v[t2].push_back(t1);
m[t1][t2]=m[t2][t1]=ton;
}
scanf("%s%s",s,e);
int t1=index(s);
int t2=index(e);
printf("%d tons\n",spfa(t1,t2));
printf("\n");
}
//fclose(stdin);
//fclose(stdout);
return ;
}
spfa
方法二:floyd
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
int n,r,m[maxn][maxn],cas=,ton,num;
char city[maxn][];
char s[],e[];
int index(char *ss)
{
int i;
for(i=;i<num;i++)
{
if(strcmp(ss,city[i])==)
{
return i;
}
}
strcpy(city[num++],ss);
return i;
}
void floyd()
{
for(int k=;k<n;k++)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
m[i][j]=max(m[i][j],min(m[i][k],m[k][j]));
}
}
}
}
void ini()
{
num=;
for(int i=;i<n;i++)
{
strcpy(city[i],"\0");
for(int j=;j<n;j++)
{
if(i==j) m[i][j]=INF;
else m[i][j]=;
}
}
}
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&r)==)
{
if(n==&&r==) break;
printf("Scenario #%d\n",++cas);
ini();
for(int i=;i<r;i++)
{
scanf("%s%s%d",s,e,&ton);
int t1=index(s);
int t2=index(e);
m[t1][t2]=m[t2][t1]=ton;
}
floyd();
scanf("%s%s",s,e);
int t1=index(s);
int t2=index(e);
printf("%d tons\n",m[t1][t2]);
printf("\n");
}
//fclose(stdin);
//fclose(stdout);
return ;
}
floyd
poj2263 zoj1952 Heavy Cargo(floyd||spfa)的更多相关文章
- POJ2263 Heavy Cargo
Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4004 Accepted: 2124 Descr ...
- POJ 2263 Heavy Cargo(Floyd + map)
Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3768 Accepted: 2013 Descr ...
- ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)
这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...
- Heavy Cargo POJ 2263 (Floyd传递闭包)
Description Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their lat ...
- poj 1847( floyd && spfa )
http://poj.org/problem?id=1847 一个水题,用来熟悉熟悉spfa和floyd的. 题意:有m条的铁路,要从x,到y, 之后分别就是条铁路与其他铁路的交点.第一个输入的为有n ...
- K - Heavy Cargo dijkstar
来源poj2263 Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their lates ...
- poj1847 Tram(Dijkstra || Floyd || SPFA)
题目链接 http://poj.org/problem?id=1847 题意 有n个车站,编号1~n,每个车站有k个出口,车站的出口默认是k个出口中的第一个,如果不想从默认出口出站,则需要手动选择出站 ...
- hdoj2544 最短路(Dijkstra || Floyd || SPFA)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路 最短路算法模板题,求解使用的Dijkstra算法.Floyd算法.SPFA算法可以当做求解 ...
- [APIO2017]商旅——分数优化+floyd+SPFA判负环+二分答案
题目链接: [APIO2017]商旅 枚举任意两个点$(s,t)$,求出在$s$买入一个物品并在$t$卖出的最大收益. 新建一条从$s$到$t$的边,边权为最大收益,长度为原图从$s$到$t$的最短路 ...
随机推荐
- Nginx和php-fpm部署到不同的服务器
Nginx安装滤过,基本上nginx上的配置很少,只要添加个server就可以了,主要安装php-fpm服务 php7.1.3安装 1.安装依赖的软件包yum -y install gcc gcc-c ...
- C#(ASP.NET)隐藏或显示Excel中指定列
今天写的一个方法,实现Excel指定列的隐藏和显示: 环境:VS2010,OFFICE 2010 代码:#region 隐藏和显示Excel中的一列 /// <summary> ...
- loadrunner之脚本篇——代理录制
版本:Loadruner 11.0 A.PC端录制Web应用程序 步骤1:根据实际情况,选择对应的协议 本例中选择Web(HTTP/HTML),如下 步骤2:找到代理设置界面 点击 Start Rec ...
- time函数计算时间
学过C语言的都知道有个time函数可以计算时间, 也好像知道time(NULL)返回的是一个距离1970年1月1日0时0分0秒的秒数. #include <stdio.h> #includ ...
- json教程系列(2)-生成JSONObject的方法
生成JSONObject一般有两种方式,通过javabean或者map类型来生成.如下面的例子: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...
- ggplot2学习总结
- 自己的第一个MapReduce程序
数据源:来自互联网招聘hadoop岗位的薪资数据,其中几行示例数据如下: 美团 3-5年经验 15-30k 北京 [够牛就来]hadoop高级工程... 北信源 3-5年经验 15-20k 北京 Ja ...
- Plist文件与数据解析
综述 初步阶段当我们做个需要点数据的练习时(比如购物商品展示),我们可能是将数据直接写在代码中,比如说定义一个字符串数组或存放字典的数组.但这其实并不是一种合理的做法.因为如果当数据修改的时候,就要经 ...
- MongoDB快速入门(二)- 数据库
创建数据库 MongoDB use DATABASE_NAME 用于创建数据库.该命令如果数据库不存在,将创建一个新的数据库, 否则将返回现有的数据库. 语法 use DATABASE语句的基本语法如 ...
- LCN协调者服务集群
官方文档: https://github.com/codingapi/tx-lcn/wiki/TxManager%E9%9B%86%E7%BE%A4%E8%AF%B4%E6%98%8E 核心原理 通过 ...