[问题] UVa11865 Directed Graph的MST

楼主: darrenlee1 (darrenleeleelee)   2020-06-22 00:39:00
这题我卡好久TLE,这题是directed graph要找minimum spanning tree,加上他
time limit只给1秒,用了二分搜的方法我还是一直TLE,请问我还有哪边可以优化吗?
题目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=0&problem=2965&mosmsg=Submission+received+with+ID+25162727
#include <bits/stdc++.h>
using namespace std;
const int maxn = 60+5;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from, to, cap, cost;
};
Edge E[maxn * maxn], e[maxn * maxn];
int n, m, c;
int in[maxn], pre[maxn], id[maxn], vis[maxn];
int CLE(int root, int n, int m, int lowest_b)
{
int res = 0;
while(1)
{
for(int i = 0; i < n; i++){
in[i] = INF;
}
for(int i = 0; i < m; i++){
int from = e[i].from, to = e[i].to;
if(from != to && e[i].cap >= lowest_b && e[i].cost < in[to]){
in[to] = e[i].cost;
}
pre[to] = from;
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(in[i] == INF) return -1;
}
int num = 0;
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(vis));
in[root] = 0;
for(int i = 0; i < n; i++){
res += in[i];
int v = i;
while(vis[v] != i && id[v] == -1 && v != root)
{
vis[v] = i;
v = pre[v];
}
if(v != root && id[v] == -1)
{
for(int j = pre[v]; j != v; j = pre[j]){
id[j] = num;
}
id[v] = num++;
}
}
if(num == 0) break;
for(int i = 0; i < n; i++){
if(id[i] == -1) id[i] = num++;
}
for(int i = 0; i < m; i++){
int from = e[i].from, to = e[i].to;
e[i].from = id[from]; e[i].to = id[to];
if(id[from] != id[to]) e[i].cost -= in[to];
}
n = num;
root = id[root];
}
return res;
}
int main(int argc, char const *argv[])
{
int T; scanf("%d", &T);
while(T
作者: DJWS (...)   2020-06-25 16:41:00
楼主: darrenlee1 (darrenleeleelee)   2020-06-27 09:43:00
感觉程式码有问题
作者: alan23273850   2020-06-29 09:40:00
这种经典老题没有标准最佳解吗
作者: DJWS (...)   2020-07-15 07:42:00
有 甚至google就有别人写好的程式码如果原po认为不是算法有问题 而是实作有问题 那么我建议你可以拿别人写好的程式码 仔细比较差异"如何实作算法"目前尚未出现任何有系统的知识 因此 没有人可以明确讲出你的程式码应该如何改进 你只能苦工比对

Links booklink

Contact Us: admin [ a t ] ucptt.com