关闭

hdoj 3986 Harry Potter and the Final Battle

标签: trainingstruct算法up
153人阅读 评论(0) 收藏 举报
分类:

类型:最短路

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3986

来源:2011 Multi-University Training Contest 15 - Host by WHU

思路:伏地魔可以破坏任何一条边,当他破坏的边不是最短路上的边的时候,哈利波特可以选择走最短路。当破坏最短路上的边的时候,为保证最坏情况下的最短路,枚举删除最短路径上的边,再分别求最短路,取其最大值[即最坏情况]。在spfa算法中用pre数组记录最短路径上的边,可以这样想,每松弛一次,更新松弛该节点的边,最终pre保留的即是最短路径上的边

// hdoj 3986 Harry Potter and the Final Battle
// ac 328MS 2188K
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <set>
#include <deque>
#include <bitset>
#include <functional>
#include <utility>
#include <iomanip>
#include <cctype>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define max(a,b) ((a) > (b)) ? (a) : (b)
#define min(a,b) ((a) < (b)) ? (a) : (b)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)

const int MAXN = 1010;
const int MAXM = 100010;
const int hash_size = 25000002;
const int INF = 0x7f7f7f7f;

bool vis[MAXN];
int n, m;
int cnt;
int ttt;
int cnt0[MAXN];
int dist[MAXN];
int head[MAXN];
int pre[MAXM];
int edge[MAXN];

struct node {
	int u, v, nxt, w;
}p[MAXM];

void addedge(int u, int v, int w) {
    p[cnt].u = u;
	p[cnt].v = v;
	p[cnt].w = w;
	p[cnt].nxt = head[u];
	head[u] = cnt++;
	p[cnt].u = v;
	p[cnt].v = u;
	p[cnt].w = w;
	p[cnt].nxt = head[v];
	head[v] = cnt++;
}

int spfa() {
    int i;

    CLR(cnt0, 0);
    CLR(vis, false);
	FORE(i, 1, n)
		dist[i] = INF;
    dist[1] = 0;
    queue<int> q;
    q.push(1);
    vis[1] = true;
    ++cnt0[1];
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(i = head[u]; i != -1; i = p[i].nxt) {
            int v = p[i].v;
            if(p[i].w + dist[u] < dist[v]) {
                //!!!
                pre[v] = i;
                dist[v] = p[i].w + dist[u];
                if(!vis[v]) {
                    q.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    return dist[n];
}


int main() {
	int cas;
	int i;
	int u, v, w;

	scanf("%d", &cas);
	while(cas--) {
		scanf("%d %d", &n, &m);
		cnt = 2;
		CLR(head, -1);
		CLR(pre, -1);
		FORE(i, 1, m) {
			scanf("%d %d %d", &u, &v, &w);
			addedge(u, v, w);
		}
		ttt = 0;
		int end_max = spfa();
		u = n;
		while(pre[u] != -1) {
		    edge[ttt++] = pre[u];
		    u = p[pre[u]].u;
		}
		for(i = 0; i < ttt; ++i) {
			int tmp = p[edge[i]].w;
//			p[jilu[i]].w = INF;
//			p[jilu[i] ^ 1].w = INF;
            p[edge[i]].w = INF;
            p[edge[i] ^ 1].w = INF;
			int tmp_max = spfa();
			if(end_max < tmp_max)
				end_max = tmp_max;
			p[edge[i]].w = tmp;
			p[edge[i] ^ 1].w = tmp;
		}
		if(end_max == INF)
			printf("-1\n");
		else
			printf("%d\n", end_max);
	}
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:52039次
    • 积分:1725
    • 等级:
    • 排名:千里之外
    • 原创:128篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论