[問題] 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