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$的最短路 ...
随机推荐
- ModelSim之TCL仿真
在使用ModelSim时,我们一般都是从界面UI进行操作的,这样也比较直观易学.但是在很多的调试时,发现很多操作都是重复的,修改一下代码就要再次进行相关操作,这样很没有效率.其实,ModelSim是可 ...
- mysql数据库补充知识2 查询数据库记录信息之单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...
- 剑指offer 面试62题
面试62题: 题目:圆圈中最后剩下的数字 题:0,1,...,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 解题思路:约瑟夫环问题,可 ...
- LDAP注入
理解LDAP与LDAP注入 0x01 LDAP简介 查阅了一些网上的资料,读了好久还是觉得理解起来很困难,感觉还是不够干,之后看到的一个博客http://www.chinaunix.net/old_j ...
- des加密——补齐
下面这个网址(英文)介绍的比较全面. http://www.di-mgt.com.au/cryptopad.html
- jsp导出
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【leetcode刷题笔记】Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Hearbeat 介绍
Hearbeat 介绍 Linux-HA的全称是High-Availability Linux,它是一个开源项目,这个开源项目的目标是:通过社区开发者的共同努力,提供一个增强linux可靠性(reli ...
- 【P2422】良好的感觉(单调栈优化DP//奇怪的暴力)
话说正解是单调栈优化DP,然而貌似根据某种玄学的推算,这个题暴力出解貌似也是可以的.首先,我们枚举所有的点作为最小点,然后横向展开,遇到更小的就停止...然后再操作一下,看上去时间O(N^2),然而由 ...
- sql server 2005 Express 下载
简体中文版: SQL Server 2005 Express Edition 简体中文版 链接页面: http://www.microsoft.com/downloads/details.aspx?d ...