POJ 1797 Heavy Transportation

 /*
dijstra的变体,求从1到n的所有路径上最小权值的最大值
第一次用STL的priority_queue,以前都是自己手写堆,效率
不是很理想,看来以后还得自己手写

6131486 bobten2008 1797 Accepted 9464K 454MS C++ 2140B 2009-11-15 19:42:25

*/
#include <iostream> #include <queue> #define minv(a, b) ((a) <= (b) ? (a) : (b)) #define maxv(a, b) ((a) >= (b) ? (a) : (b)) #define MAX_N 1000 using namespace std; int noden, edgen; struct qnode { int id; int minVal; }; struct node { int countv, minVal; bool v; int con[MAX_N + 1][2]; }nodes[MAX_N + 1]; struct compare { bool operator()(qnode * n1, qnode * n2) { return n1->minVal <= n2->minVal; } }; priority_queue<qnode*, vector<qnode*>, compare> pq; int maxW; void dij() { int i, fromid, toid, weight; qnode *curNode; while(!pq.empty()) { curNode = pq.top(); pq.pop(); fromid = curNode->id; if(nodes[fromid].v) continue; nodes[fromid].v = true; int connum = nodes[fromid].countv; for(i = 1; i <= connum; i++) { toid = nodes[fromid].con[i][0]; if(!nodes[toid].v) { weight = nodes[fromid].con[i][1]; weight = minv(nodes[fromid].minVal, weight); if(nodes[toid].minVal < weight) { nodes[toid].minVal = weight; qnode *newnode = new qnode(); newnode->id = toid; newnode->minVal = weight; pq.push(newnode); } if(toid == noden) maxW = maxv(maxW, nodes[toid].minVal); } } delete curNode; } } int main() { int caseN, c, i; scanf("%d", &caseN); for(c = 1; c <= caseN; c++) { memset(nodes, 0, sizeof(nodes)); maxW = INT_MIN; while(!pq.empty()) pq.pop(); scanf("%d%d", &noden, &edgen); for(i = 1; i <= noden; i++) { qnode *newnode = new qnode(); newnode->id = i; if(i == 1) newnode->minVal = INT_MAX; else newnode->minVal = 0; pq.push(newnode); nodes[i].minVal = newnode->minVal; } int from, to, weight; for(i = 1; i <= edgen; i++) { scanf("%d%d%d", &from, &to, &weight); nodes[from].countv++; nodes[from].con[nodes[from].countv][0] = to; nodes[from].con[nodes[from].countv][1] = weight; nodes[to].countv++; nodes[to].con[nodes[to].countv][0] = from; nodes[to].con[nodes[to].countv][1] = weight; } dij(); printf("Scenario #%d:/n", c); printf("%d/n/n", maxW); } return 0; } 

你可能感兴趣的:(POJ 1797 Heavy Transportation)