# hdoj 3986 Harry Potter and the Final Battle

153人阅读 评论(0)

// 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 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].u = v;
p[cnt].v = u;
p[cnt].w = w;
}

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(pre, -1);
FORE(i, 1, m) {
scanf("%d %d %d", &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条
文章分类
阅读排行
评论排行
最新评论