社区讨论
玄关,这道题真的用不了最短路吗?
P6111 [USACO18JAN] MooTube S参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @m0b1wead
- 此快照首次捕获于
- 2024/08/26 21:45 2 年前
- 此快照最后确认于
- 2024/08/27 09:39 2 年前
写了个dij结果TLE最后三个
求问这一题能不能用最短路,能的话求求看看我这个代码怎么改,不能的话……
CPP/*
Luogu P6111 MooTube S
https://www.luogu.com.cn/problem/P6111
*/
#include "iostream"
#include "cstring"
#include "queue"
#define pii pair<int,int>
using namespace std;
int n, Q, head[5005], idx, k, v, temp1, temp2, temp3, ans1, ans2;
int dis[5005];
bool book[5005], flag[5005];
struct edge{
int to, nxt, w;
}e[100005];
void add_edge(int u, int v_, int w){
e[++idx].to=v_;
e[idx].w=w;
e[idx].nxt=head[u];
head[u]=idx;
}
class cmp{
public:
bool operator() (pii a,pii b){
return b.second >= a.second;
}
};
void dijkstra(int st){
memset(dis, 0x3f, sizeof(dis)), memset(flag, 0, sizeof(flag));
ans1=0, flag[st]=true;
priority_queue<pii, vector<pii>, cmp>q;
q.push(make_pair(st, dis[st]));
while(!q.empty()){
int temp=q.top().first, to;
book[temp]=false;
q.pop();
for(int i=head[temp];i;i=e[i].nxt){
to=e[i].to;
if(min(dis[temp], e[i].w) < dis[to]){
dis[to]=min(dis[temp], e[i].w);
if(dis[to]>=k && !flag[to]) ans1++; flag[to]=true;
if(!book[to]){
book[to]=true;
q.push(make_pair(to, dis[to]));
}
}
}
}
}
int main(){
scanf("%d%d", &n, &Q);
for(int i=1;i<n;i++){
scanf("%d%d%d", &temp1, &temp2, &temp3);
add_edge(temp1, temp2, temp3);
add_edge(temp2, temp1, temp3);
}
while(Q--){
scanf("%d%d", &k, &v);
dijkstra(v);
printf("%d\n", ans1);
}
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...