HDU 3986 Harry Potter and the Final Battle 最短路

题目大意: 给你一个无向图, 蓝厚让你删掉一条边, 让剩下的图中的最短路 最长, maxmin系列。。

一开始想不出来解法, 就准备暴力一发试试, 也就是找到最短路之后一条条拆边, 然后不出意外的T了, 以为有什么厉害的算法, 结果赛后发现- -只因为删边的时候没加

if( ans == inf) break; 这一句, 啊啊啊居然卡这种数据我也是醉了, 然后就噩梦

#include "iostream"
#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "climits"
#include "queue"
#include "cmath"
#include "map"
#include "set"
#include "stack"
#include "vector"
#include "sstream"
#include "algorithm"
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1010;
const int maxm = 1e5 + 50;
typedef long long ll;

int dis[maxn],pnt[maxm],nxt[maxm],vis[maxn],cost[maxm];
int cnt,head[maxn],p[maxn],n;
void add(int u, int v, int w) {
    pnt[cnt] = v;
    nxt[cnt] = head[u];
    cost[cnt] = w;
    head[u] = cnt++;
}

int spfa(int id) {
    memset(dis, 0x3f, sizeof dis);
    dis[1] = 0;
    queue<int> q;
    q.push(1);
    while (q.size()) {
        int u = q.front();q.pop();
        vis[u] = 0;
        for (int i = head[u]; ~i; i = nxt[i]) {
            int v = pnt[i];
            if (dis[v] <= dis[u] + cost[i] || i == id) continue ;
            dis[v] = dis[u] + cost[i];
            p[v] = i;
            if (!vis[v]) q.push(v);
            vis[v] = 1;
        }
    }
    return dis[n];
}

inline int read() {
    char c = getchar();
    while (!isdigit(c)) c = getchar();
    int x = 0;
    while (isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

int main()
{
#ifdef LOCAL
	freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//      freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
#endif
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int m,u,v,w;
		scanf("%d%d",&n,&m);
		memset(head,-1,sizeof head);
		cnt = 0;
		for(int i=1;i<=m;i++)
		{
			u = read();v = read(); w = read();
			add(u, v, w);
			add(v, u, w);
		}
		spfa(-1);
		int now = n, ans = 0;
		while (now - 1) {
            int id = p[now];
            ans = max(ans, spfa(id));
            if (ans == inf) break;
            now = pnt[id ^ 1];
		}
		if(ans >= inf)
			cout<<"-1"<<endl;
		else
			cout<<ans<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值