打了一个最大生成树+dfs,60分成功tle

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10005;
const int maxm = 50005;
int n, m, cnt = 0;
struct edge
{
int from, to, value;
bool operator < (const edge b) const {
return this->value > b.value;
}
}es[maxm];
vector<edge> G[maxn];
struct car
{
int from, to;
}cs[30001];
int q;
void read() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
es[cnt].from = x;
es[cnt].to = y;
es[cnt].value = z;
cnt++;
}
cin >> q;
for(int i = 1; i <= q; i++) cin >> cs[i].from >> cs[i].to;
}
int fa[maxn];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void kruskal() {
sort(es, es+cnt);
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 0; i < cnt; i++) {
int x = es[i].from; int y = es[i].to;
int fx = find(x); int fy = find(y);
if(fx == fy) {
es[i].value = -1;
continue;
}
else fa[fx] = fy;
}
}
void build_tree() {
sort(es, es+cnt);
for(int i = 0; i < cnt; i++) {
if(es[i].value != -1) {
int x = es[i].from;
int y = es[i].to;
G[x].push_back({x, y, es[i].value});
G[y].push_back({y, x, es[i].value});
}
else break;
}
}
int mini = 0x3f3f3f;
int vis[maxn];
void dfs(int from, int to, int m) {
vis[from] = 1;
if(from == to) {mini = m; return;}
vector<edge>::iterator it;
for(it = G[from].begin(); it != G[from].end(); it++) {
edge &e = *it;
if(!vis[e.to]) {
dfs(e.to, to, min(m, e.value));
}
}
}
void solve() {
for(int i = 1; i <= q; i++) {
int x = cs[i].from;
int y = cs[i].to;
mini = 0x3f3f3f;
memset(vis, 0, sizeof(vis));
dfs(x, y, 0x3f3f3f);
if(mini == 0x3f3f3f) cout << -1;
else cout << mini;
cout << endl;
}
}
int main() {
// freopen("input.in", "r", stdin);
read();
kruskal();
build_tree();
solve();
return 0;
}

P1967 货车运输 -60分的更多相关文章

  1. 洛谷 P1967 货车运输

    洛谷 P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在 ...

  2. Luogu P1967 货车运输(Kruskal重构树)

    P1967 货车运输 题面 题目描述 \(A\) 国有 \(n\) 座城市,编号从 \(1\) 到 \(n\) ,城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \ ...

  3. P1967 货车运输

    P1967 货车运输最大生成树+lca+并查集 #include<iostream> #include<cstdio> #include<queue> #inclu ...

  4. 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增

    倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...

  5. 【杂题总汇】NOIP2013(洛谷P1967) 货车运输

    [洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...

  6. P1967 货车运输(倍增LCA,生成树)

    题目链接: https://www.luogu.org/problemnew/show/P1967 题目描述 A国有n座城市,编号从 1到n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制, ...

  7. 洛谷 P1967 货车运输 Label: 倍增LCA && 最小瓶颈路

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...

  8. 洛谷 P1967 货车运输(克鲁斯卡尔重构树)

    题目描述 AAA国有nn n座城市,编号从 11 1到n nn,城市之间有 mmm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qqq 辆货车在运输货物, 司机们想知道每辆车在不超过车 ...

  9. P1967 货车运输 树链剖分

    题目描述 AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最 ...

随机推荐

  1. jenkins+ant+jmeter搭建持续集成的接口测试平台

    一.jemter接口脚本的编写步骤如下: 1. 编写接口请求 通过录制或者查看接口文档,编写接口请求,进行调试,确保接口调试通过,对于http的请求来说,就是正确的填写域名,查询字符串,查询参数等信息 ...

  2. git 学习笔记2--How to create/clone a repository

    1. create/clone 1.1 create 针对已经存在的目录创建一个repository,使用以下命令: git init Initialized empty Git repository ...

  3. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  4. ubifs核心对象 -- TNC和LPT

              文件系统的核心问题是存储.这里面隐含2个问题:1)存储什么?2)存储到哪里?文件系统中的各种技术手段都是如何高效的解决这2个问题.ubifs用node标准化每一个存储对象,用lpr ...

  5. ACM: 畅通工程-并查集-解题报告

    畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查城镇交通状况 ...

  6. IP地址分类整理

    什么是IP地址? IP地址就是计算机在网络中地址. IP地址有多少个? IP地址范围是:0.0.0.0~225.225.225.255,这只是人为了方便记录才转为十进制的,ip地址实际是一个32位地址 ...

  7. HDU 4597 Play Game

    题目链接 什么都不想说,最近状态暴跌.. #include <cstdio> #include <cstring> #include <iostream> usin ...

  8. 【CodeVS】 p1225 八数码难题

    题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字 ...

  9. nohup命令浅析

    要将一个命令放到后台执行,我们一般使用nohup sh command & &都知道是放到后台执行这个命令,那么nohup是做什么的? 这就要从unix的信号说起,unix的信号机制可 ...

  10. xcode 忽然无法真机调试

    手机升级了系统后一直没有再进行真机调试,今天要去面试把手机插上后忽然显示iPhone(unavailable),选中自己的设备后运行发现弹出警告could not find developer dis ...