HDU 2121 Ice_cream’s world II 最小树形图(不定根)

在一个由N个城市和M条单向道路组成的冰激凌世界中,需要选择一个城市作为首都,并确保从首都出发能到达每一个城市。本文介绍了一个算法解决方案,通过构造一个虚拟根节点来寻找最小成本的路径集合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Ice_cream’s world II

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2290    Accepted Submission(s): 543


Problem Description
After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing.
 

Input
Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.
 

Output
If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank.
 

Sample Input
   
3 1 0 1 1 4 4 0 1 10 0 2 10 1 3 20 2 3 30
 

Sample Output
   
impossible 40 0
 

Author
Wiskey
 

Source
 

Recommend
威士忌
 
给你城市个数和可以连接的路径及花费,路径是单向传递的,让你求连接所有城市所需要的最小花费及此时的根。
首先虚拟出一个根,让这个根和所有的节点相连,权值为无穷大或者所有边的权值+1,记为max。然后用祝刘算法进行计算,算出的结果减去max,如果得出的结果为-1或者比max还要大,则不存在最小树形图。求最小根时,在找最小入弧时,如果这条弧的起点是虚拟根,那么这条弧的终点就是要求的根。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define M 10007
#define inf 0x3f3f3f
using namespace std;
int pre[M],vis[M],id[M];
int in[M];
int n,m,ansi;

struct Node//建立邻接表
{
    int u,v;
    int cost;
}E[M*M+5];

int direct_mst(int root,int nv,int ne)
{
    int ret=0;
    while(true)
    {
        //找最小入边
        for(int i=0;i<nv;i++)
            in[i]=inf;
        for(int i=0;i<ne;i++)
        {
            int u=E[i].u;
            int v=E[i].v;
            if(E[i].cost<in[v]&&u!=v)
            {
                pre[v]=u;
                if(u==root)
                    ansi=i;
                in[v]=E[i].cost;
            }
        }
        for(int i=0;i<nv;i++)
        {
            if(i==root)continue;
            if(in[i]==inf)return -1;
        }
        //找环
        int cntnode=0;
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        in[root]=0;
        for(int i=0;i<nv;i++)//标记每个环
        {
            ret+=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 u=pre[v];u!=v;u=pre[u])
                {
                    id[u]=cntnode;
                }
                id[v]=cntnode++;
            }
        }
        if(cntnode==0)break;//无环
        for(int i=0;i<nv;i++)
            if(id[i]==-1)
                id[i]=cntnode++;
        //缩点,重新标记
        for(int i=0;i<ne;i++)
        {
            int v=E[i].v;
            E[i].u=id[E[i].u];
            E[i].v=id[E[i].v];
            if(E[i].u!=E[i].v)
            {
                E[i].cost-=in[v];
            }
        }
        nv=cntnode;
        root=id[root];
    }
    return ret;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u,v,w,sum=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            E[i].u=++u;
            E[i].v=++v;
            if(u!=v)
                E[i].cost=w;
            else
                E[i].cost=inf;
            sum+=w;
        }
        sum++;
        for(int i=0;i<n;i++)
        {
            E[m+i].u=0;
            E[m+i].v=i+1;
            E[m+i].cost=sum;
        }
        int ans=direct_mst(0,n+1,m+n);//n代表点的个数,m代表边的个数
        if(ans==-1||ans-sum>=sum)
            printf("impossible\n");//无法生成最小树形图
        else
            printf("%d %d\n",ans-sum,ansi-m);
        printf("\n");
    }
    return 0;
}



标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值