Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 23914 | Accepted: 6355 |
Description
Input
Output
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
题意&分析:水题一枚,给定N个点,M条路,求从1到N的最大的最小边权,如果数据在300以内,Floyd水过,但是这个题不行,N<1000,那么,我们就可以用Dijstra来做,这里需要注意的应该就是 初始化以及顶点的松弛操作:
d[y] = max(d[y], min(d[x], Map[x][y]));
网上有给最大生成树来做,也很简单,最小生成树会的话,这个也就不再话下。
下面给出两种风格的Dij代码:
/****************************>>>>HEADFILES<<<<****************************/ #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; /****************************>>>>>DEFINE<<<<<*****************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #define FIN freopen("input.txt","r",stdin) #define rep(i,a,b) for(int i = a;i <= b;i++) #define MP(a,b) make_pair(a,b) #define PB(a) push_back(a) #define fst first #define snd second /****************************>>>>>>DEBUG<<<<<<****************************/ #define out(x) cout<<x<<" "; /****************************>>>>SEPARATOR<<<<****************************/ const int maxn = 1000 + 5; const int INF = 0x3f3f3f3f; int N, M, Map[maxn][maxn], d[maxn]; bool vis[maxn]; void init() { memset(Map, 0, sizeof(Map)); } void Dijkstra() { memset(vis, false, sizeof(vis)); d[1] = 0; rep(i,2,N) d[i] = Map[1][i]; rep(i, 1, N-1) { int x = 0, m = 0; rep(y, 1, N) if(!vis[y] && d[y] > m) m = d[x = y]; vis[x] = true; rep(y, 1, N) { if(!Map[x][y]) continue; d[y] = max(d[y], min(d[x], Map[x][y])); } } } int main() { // FIN; int T, cas = 0, x, y, t; for(scanf("%d", &T); T--;) { scanf("%d %d", &N, &M); init(); rep(i, 1, M) { scanf("%d %d %d", &x, &y, &t); Map[x][y] = Map[y][x] = t; } Dijkstra(); if(cas) puts(""); printf("Scenario #%d:\n", ++cas); printf("%d\n", d[N]); } return 0; }
/****************************>>>>HEADFILES<<<<****************************/ #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; /****************************>>>>>DEFINE<<<<<*****************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #define FIN freopen("input.txt","r",stdin) #define rep(i,a,b) for(int i = a;i <= b;i++) #define rep0(i,b) for(int i = 0;i < b;i++) #define rep1(i,b) for(int i = 1;i <= b;i++) #define MP(a,b) make_pair(a,b) #define PB(a) push_back(a) #define fst first #define snd second /****************************>>>>>>DEBUG<<<<<<****************************/ #define out(x) cout<<x<<" "; /****************************>>>>SEPARATOR<<<<****************************/ const int maxn = 1000 + 5; const int INF = 0x3f3f3f3f; int N,M; struct Edge { int from, to, dist; Edge() {} Edge(int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist) {} }; struct HeapNode { int pos, d; HeapNode() {} HeapNode(int _pos, int _d) : pos(_pos), d(_d) {} bool operator < (const HeapNode & p) const { return d < p.d; } }; struct Dijkstra { int n, s, d[maxn]; bool vis[maxn]; vector<Edge> edges; vector<int> G[maxn]; Dijkstra() {} Dijkstra(int _n, int _s) : n(_n), s(_s) {} void init(int _n, int _s) { this->n = _n, this->s = _s; edges.clear(); rep1(i, n) { G[i].clear(); } } void AddEdge(int u, int v, int t) { edges.PB(Edge(u, v, t)); int m = edges.size(); G[u].PB(m - 1); } void dijkstra() { memset(vis, false, sizeof(vis)); memset(d,0,sizeof(d)); d[s] = INF; priority_queue<HeapNode> Que; Que.push(HeapNode(s, 0)); while(!Que.empty()) { HeapNode now = Que.top(); Que.pop(); int u = now.pos; if(vis[u]) continue; vis[u] = true; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(d[e.to] < min(d[u], e.dist)) { d[e.to] = min(d[u], e.dist); Que.push(HeapNode(e.to,d[e.to])); } } } } }dij; int main() { // FIN; int T,cas = 0,x,y,t; for(scanf("%d",&T);T--;) { scanf("%d %d",&N,&M); dij.init(N,1); rep(i,1,M) { scanf("%d %d %d",&x,&y,&t); dij.AddEdge(x,y,t); dij.AddEdge(y,x,t); } dij.dijkstra(); if(cas) puts(""); printf("Scenario #%d:\n",++cas); printf("%d\n",dij.d[N]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
poj 1797 Heavy Transportation 【最短路Dijkstra 变式】
原文地址:http://blog.csdn.net/acmore_xiong/article/details/47291061