uva11183最小树形图

本来看数据用临界矩阵可能会超时,还是写了临界矩阵,结果1A了

模板的不能再模板 了

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=1000+10,maxn=100000+10,inf=0x3f3f3f;

int w[N][N];
int n,m;
bool vis[N],in[N];
int pre[N];
void dfs(int u)
{
    vis[u]=1;
    for(int i=0;i<n;i++)
        if(!vis[i]&&w[u][i]<inf)
           dfs(i);
}
int dirmst(int u)
{
    int ans=0;
    dfs(u);
    for(int i=0;i<n;i++)
        if(!vis[i])
           return -1;
    memset(vis,0,sizeof vis);
    while(1){
        for(int i=0;i<n;i++)
        {
            if(i!=u&&!in[i])
            {
                pre[i]=i;
                w[i][i]=inf;
                for(int j=0;j<n;j++)
                    if(!in[j]&&w[j][i]<w[pre[i]][i])
                        pre[i]=j;
            }
        }
        int i;
        for(i=0;i<n;i++)
        {
            if(!in[i]&&i!=u)
            {
                int j=i,cnt=0;
                while(pre[j]!=i&&cnt<=n&&j!=u)j=pre[j],cnt++;
                if(cnt>n||j==u)continue;
                break;
            }
        }
        if(i>n-1)
        {
            for(int i=0;i<n;i++)
                if(!in[i]&&i!=u)
                   ans+=w[pre[i]][i];
            return ans;
        }
        int j=i;
        memset(vis,0,sizeof vis);
        do{
            ans+=w[pre[j]][j],j=pre[j],vis[j]=in[j]=1;
        }while(j!=i);
        in[i]=0;
        for(int k=0;k<n;k++)
            if(vis[k])
                for(int j=0;j<n;j++)
                   if(!vis[j])
                   {
                       if(w[i][j]>w[k][j])w[i][j]=w[k][j];
                       if(w[j][k]<inf&&w[j][k]-w[pre[k]][k]<w[j][i])
                          w[j][i]=w[j][k]-w[pre[k]][k];
                   }
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t,cnt=0;
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            vis[i]=in[i]=0;
            for(int j=0;j<n;j++)
                w[i][j]=inf;
        }
        while(m--){
            int a,b,c;
            cin>>a>>b>>c;
            if(a==b)continue;
            if(w[a][b]>c)w[a][b]=c;
        }
        /*for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                cout<<w[i][j]<<" ";
            cout<<endl;
        }*/
        int ans=dirmst(0);
        if(ans<0)cout<<"Case #"<<++cnt<<": Possums!"<<endl;
        else cout<<"Case #"<<++cnt<<": "<<ans<<endl;
    }
    return 0;
}

时间: 2024-08-04 18:17:49

uva11183最小树形图的相关文章

UVa11183 - Teen Girl Squad(最小树形图-裸)

Problem I Teen Girl Squad Input: Standard Input Output: Standard Output -- 3 spring rolls please. -- MSG'D!! -- Oh! My stomach lining! Strong Bad You are part of a group of n teenage girls armed with cellphones. You have some news you want to tell ev

UVa11183 Teen Girl Squad, 最小树形图,朱刘算法

Teen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n teenage girls armed with cellphones. You have some news you want to tell everyone in the group. The problem is that no two of you are in the same room, and you

UVA-11183 Teen Girl Squad (最小树形图、朱刘算法模板)

题目大意:给一张无向图,求出最小树形图. 题目分析:套朱-刘算法模板就行了... 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; # define LL long long # define REP(i,s,n) for(int i=s;i<n;++i) # define CL(a,b) me

最小树形图模板 UVA11183

题意:给定n个节点m条边的有向带权图,求以0为根节点的最小树形图权值大小 1 #include<cstdio> 2 #include<algorithm> 3 #include<string.h> 4 using namespace std; 5 const int maxn=1e3+10; 6 const int maxm=4e4+10; 7 const int inf=0x3f3f3f3f; 8 int n,m; 9 int cost[maxn],pre[maxn]

bzoj4349: 最小树形图

最小树形图模板题…… 这种\(O(nm)\)的东西真的能考到么…… #include <bits/stdc++.h> #define N 60 #define INF 1000000000 using namespace std; int n, m, nn; double ai[N], an[N], ci[2][N][N], ans; int bc[N]; int ini[N], vis[N], inc[N], inl[N]; int dfn; int dfs(int t) { vis[t]

Directed_MST 最小树形图

List Directed_MST 最小树形图 List Knowledge 模板 Practice 参考资料 Knowledge 求一个图中遍历到每个点的方案中的最小边权和,显然n-1条边,即一颗树即可. 最小生成树?当然这里不是的,这里的最小树形图算法是针对有向图的. 最小树形图的第一个算法是1965年朱永津和刘振宏提出的复杂度为O(VE)的算法.简称朱刘算法. 1986年, Gabow, Galil, Spencer和Tarjan提出了一个复杂度更好的实现,其时间复杂度为O(E+VlogV

hdu2121+不定根最小树形图

算和定根最小树形图相同. 我们只需:设一个权值sumw=所有边之和+1,类似于网络流,向图中加入一个超级源点,把这个点作为虚根.虚根到其他所有点之间连上一条边,边权值为sumw. 求出的值减去sumw即为最小树形图的权值. 当然,返回-1则无解.此外,当求出的值>=2*sumw,也是无解的. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 struct node 5 { 6 int u,v; 7

HDOJ 2121 Ice_cream’s world II 最小树形图无根树

朱刘算法 最小树形图无根树: 建立一个虚拟的根节点,向所有节点连边,权值为其他所有边的权值和+1 在求最小树形图的时候,记录和虚拟的根相连的是哪个节点 在这题中,边是从小往大加的所以直接记录的是相连的是第几号边.... Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3442    Accept

最小树形图【模板】

基于贪心和缩点的思想. 假设根的顶点是V0. (1)除了根结点外,所有的点Vi,找到以Vi为终点的最短的边,加入集合中 (pre[v]存放的是终点v的起点,In[v]存放终点为v的最短的边) (2)检查集合中有没有有向环和收缩点.若没有有向环和收缩点,结束计算:若没有有向环.但含收缩边,则跳至步骤(4):若含有有向环,则跳至步骤(3).Ps:如果出现重边,将忽略权值较高的 (3)含有有向环,则收缩有向环(缩点),把有向环收缩为一个点,其有向环内的边被收缩掉,而环外的边被保 留,构建新图,重复步骤