P1967 货车运输 -60分
打了一个最大生成树+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分的更多相关文章
- 洛谷 P1967 货车运输
洛谷 P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在 ...
- Luogu P1967 货车运输(Kruskal重构树)
P1967 货车运输 题面 题目描述 \(A\) 国有 \(n\) 座城市,编号从 \(1\) 到 \(n\) ,城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \ ...
- P1967 货车运输
P1967 货车运输最大生成树+lca+并查集 #include<iostream> #include<cstdio> #include<queue> #inclu ...
- 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增
倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...
- 【杂题总汇】NOIP2013(洛谷P1967) 货车运输
[洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...
- P1967 货车运输(倍增LCA,生成树)
题目链接: https://www.luogu.org/problemnew/show/P1967 题目描述 A国有n座城市,编号从 1到n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制, ...
- 洛谷 P1967 货车运输 Label: 倍增LCA && 最小瓶颈路
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...
- 洛谷 P1967 货车运输(克鲁斯卡尔重构树)
题目描述 AAA国有nn n座城市,编号从 11 1到n nn,城市之间有 mmm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qqq 辆货车在运输货物, 司机们想知道每辆车在不超过车 ...
- P1967 货车运输 树链剖分
题目描述 AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最 ...
随机推荐
- UVa1515 Pool construction(最小割)
题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- FastDFS原理
转自:http://blog.chinaunix.net/uid-20196318-id-4058561.html 开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器 ...
- Java NIO之选择器Selector
在单独的线程中,检查多个通道是否可以进行IO操作. Selector创建:静态工厂方法创建 Selector selector = Selector.open(); 注册通道 channel.conf ...
- gulp完全开发指南 => 快来换掉你的Grunt吧
最近一直在构建Angular应用,通过bower管理前端包依赖,然后通过gulp和它配合.发现gulp相比于grunt真的很轻,现在我的项目中已经取代了grunt.这里把我的一些实践贴记录下来和大家分 ...
- Android PhoneGap 利用 Activity 实现 CordovaInterface
1.修改main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- Nginx 利用 X-Accel-Redirect response.setHeader 控制文件下载
nginx.conf location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP ...
- Coder-Strike 2014 - Round 1 C. Pattern
题目的意思是给出n个长度相同的字符串然后找出与他们匹配的字符串 将字符串存入类似二维数组的里面,每一行代表一个字符串,遍历每列,判断每列是否有公共的匹配字符,如果有输出任意一个 如果没有输出'?' # ...
- 构建json数据post到接口的若干条规则
接受数据接口: public ActionResult PostDownloadLog(PostDownloadLog postDownloadLogs) PostDownLoadLogL类 publ ...
- (转)KeyDown、KeyUp、KeyPress区别
Windows窗体通过引发键盘事件来处理键盘输入以响应Windows消息,大多数Windows窗体应用程序都通过处理键盘事件来以独占方式处理键盘输入. 1.按键的类型 Windows窗体将键盘输入标 ...
- 李洪强iOS经典面试题133--UNIX常用命令
可能碰到的iOS笔试面试题(3)--UNIX常用命令 做开发说用不到命令行,那肯定是不可能的.所以记住几个常用的命令还是很有用. cd 改变工作目录 pwd 输出当前工作目录的绝对路径在UNIX中要执 ...