POJ 2263 Heavy Cargo 多种解法
来源:互联网 发布:软件开发方法学 编辑:程序博客网 时间:2024/04/29 15:52
好题。这题可以有三种解法: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 100007struct node{ int ind,wt; bool operator < (const node &a)const { return wt<a.wt; }};int start,endi,n,m,res;int way[203][203],vis[203][203];map<string,int> mp;priority_queue<node> que;void BFS(){ memset(vis,0,sizeof(vis)); int i,maxi = 0,id; node now,next; for(i=1;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 = 0; 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=1;i<=n;i++) { if(way[now.ind][i] && !vis[now.ind][i]) { vis[now.ind][i] = vis[i][now.ind] = 1; 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 = 1,i,j,w,num; string city1,city2; node ka,kb; while(scanf("%d%d",&n,&m)!=EOF && (n||m)) { mp.clear(); num = 1; memset(way,0,sizeof(way)); for(i=0;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 0;}
0 0
- POJ 2263 Heavy Cargo 多种解法
- POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- POJ:2263 Heavy Cargo
- POJ-2263 Heavy Cargo
- POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- [floyd]POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo(floyd)
- POJ 2263 Heavy Cargo(图的遍历)
- POJ 2263 Heavy Cargo(Dijkstra算法)
- poj 2263 Heavy Cargo(最短路+floyd)
- POJ 2263 Heavy Cargo 最短路dijkstra
- POJ 2263 Heavy Cargo(Floyd变形)
- POJ 2263 Heavy Cargo(ZOJ 1952)
- UVA 11992 Fast Matrix Operations (二维线段树)
- POJ 2823 Sliding Window 再探单调队列
- c++必懂的基本概念
- FZU1894 志愿者选拔 --单调队列
- POJ 3250 Bad Hair Day --单调栈(单调队列?)
- POJ 2263 Heavy Cargo 多种解法
- POJ 1442 Black Box -优先队列
- HDU 1896 Stones --优先队列+搜索
- Variant does not reference an auomation object
- UVA 12266 Stock prices --优先队列
- UVA 11235 Frequent Values ---RMQ
- UVA 11573 Ocean Currents --BFS+优先队列
- POJ 3264 Balanced Lineup -- RMQ或线段树
- UVA 439 Knight Moves --DFS or BFS