POJ 2263 Heavy Cargo 多种解法
好题。这题可以有三种解法:1.Dijkstra 2.优先队列 3.并查集
我这里是优先队列的实现,以后有时间再用另两种方法做做。。方法就是每次都选当前节点所连的权值最大的边,然后BFS搜索。
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <queue>
- #include <functional>
- using namespace std;
- #define N 100007
- struct node
- {
- int ind,wt;
- bool operator < (const node &a)const
- {
- return wt<a.wt;
- }
- };
- int start,endi,n,m,res;
- int way[][],vis[][];
- map<string,int> mp;
- priority_queue<node> que;
- void BFS()
- {
- memset(vis,,sizeof(vis));
- int i,maxi = ,id;
- node now,next;
- for(i=;i<=n;i++)
- {
- if(way[start][i] > maxi)
- {
- maxi = way[start][i];
- id = i;
- }
- }
- now.ind = id;
- now.wt = maxi;
- que.push(now);
- //printf("%d %d\n",start,endi);
- //printf("%d %d\n",now.ind,now.wt);
- res = ;
- while(!que.empty())
- {
- now = que.top();
- que.pop();
- if(now.ind == endi)
- {
- if(now.wt > res)
- res = now.wt;
- while(!que.empty())
- que.pop();
- return;
- }
- for(i=;i<=n;i++)
- {
- if(way[now.ind][i] && !vis[now.ind][i])
- {
- vis[now.ind][i] = vis[i][now.ind] = ;
- next.ind = i;
- next.wt = min(now.wt,way[now.ind][i]);
- que.push(next);
- //printf("%d %d\n",next.ind,next.wt);
- }
- }
- }
- }
- int main()
- {
- int cs = ,i,j,w,num;
- string city1,city2;
- node ka,kb;
- while(scanf("%d%d",&n,&m)!=EOF && (n||m))
- {
- mp.clear();
- num = ;
- memset(way,,sizeof(way));
- for(i=;i<m;i++)
- {
- cin>>city1;
- cin>>city2;
- scanf("%d",&w);
- if(!mp[city1])
- mp[city1] = num++;
- if(!mp[city2])
- mp[city2] = num++;
- way[mp[city1]][mp[city2]] = w;
- way[mp[city2]][mp[city1]] = w;
- }
- cin>>city1>>city2;
- start = mp[city1];
- endi = mp[city2];
- BFS();
- printf("Scenario #%d\n",cs++);
- printf("%d tons\n\n",res);
- }
- return ;
- }
POJ 2263 Heavy Cargo 多种解法的更多相关文章
- POJ 2263 Heavy Cargo(Floyd + map)
Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3768 Accepted: 2013 Descr ...
- POJ 2263 Heavy Cargo(ZOJ 1952)
最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...
- POJ2263 Heavy Cargo
Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4004 Accepted: 2124 Descr ...
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- 多种解法解决n皇后问题
多种解法解决n皇后问题 0x1 目的 深入掌握栈应用的算法和设计 0x2 内容 编写一个程序exp3-8.cpp求解n皇后问题. 0x3 问题描述 即在n×n的方格棋盘上,放置n个皇后,要求每 ...
- 【BZOJ4555】求和(多种解法混合版本)
[BZOJ4555]求和(多种解法混合版本) 题面 BZOJ 给定\(n\),求 \[f(n)=\sum_{i=0}^{n}\sum_{j=0}^{i}S(i,j)\times 2^j \times ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
- 巴塞尔问题(Basel problem)的多种解法
巴塞尔问题(Basel problem)的多种解法——怎么计算\frac{1}{1^2}+\frac{1}{2^2}+\frac{1}{3^2}+\cdots112+122+132+⋯ ? (PS:本 ...
随机推荐
- (旧)子数涵数·C语言——让C帮你做计算
之前,我们学过了我们的第一个C程序--hello World.现在开始进一步学习,想一想如何让C帮你做计算. 我们先来看代码(我没有新建,还是用之前的hello world.cpp): 好,因为之前在 ...
- 多准则决策模型-TOPSIS方法
多准则决策–Multiple Criteria Decision Making 多准则决策–Multiple Criteria Decision Making 多准则决策是指在具有相互冲突.不可共度的 ...
- ASP.NET Web API 通过Authentication特性来实现身份认证
using System; using System.Collections.Generic; using System.Net.Http.Headers; using System.Security ...
- C++模板元编程
ABC
- CSS之绝对定位那些事
1.垂直居中 有时我们会使用margin: 0 auto;作居中使用.但有的时候我们需要垂直居中,例如在div里面垂直居中显示一张加载中的gif图. 下面这种写法就可以完美实现: 垂直居中的子容器 { ...
- 访问SAP的Domain的Value Range
访问Domain的Value Range有两种方法: 1.直接访问表 dd07l和dd07T select * from dd07l where domname = ...
- Android根据APP包名启动应用
public void openApp(String packageName, Context context) { PackageManager packageManager = context.g ...
- 实战1--应用EL表达式访问JavaBean的属性
(1)编写index.jsp页面,用来收集用户的注册信息 <%@ page language="java" pageEncoding="GBK"%> ...
- wxPython简单入门
wxPython简介 wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的 GUI 用户界面. wxPython 是作为优秀 ...
- 【读书笔记】iOS-NSDictionary与NSArray的比较
有时候为什么不用数组存储然后在数组里查询数值呢?字典(也称为散列表或关联数组)使用的是键查询的优化存储方式.它可以立即找出要查询的数据,而不需要遍历整个数组进行查找.对于频繁的查询和大型的数据集来说, ...