How Many Maos Does the Guanxi Worth (dijkstra)

本文介绍了一种算法问题,目标是最小化刘老板利用金钱关系让其孩子进入好中学的成本,或使其计划失败。通过枚举关系网中的关键节点并计算最小花费。

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

How Many Maos Does the Guanxi Worth


 
"Guanxi" is a very important word in Chinese. It kind of means "relationship" or "contact". Guanxi can be based on friendship, but also can be built on money. So Chinese often say "I don't have one mao (0.1 RMB) guanxi with you." or "The guanxi between them is naked money guanxi." It is said that the Chinese society is a guanxi society, so you can see guanxi plays a very important role in many things. 

Here is an example. In many cities in China, the government prohibit the middle school entrance examinations in order to relief studying burden of primary school students. Because there is no clear and strict standard of entrance, someone may make their children enter good middle schools through guanxis. Boss Liu wants to send his kid to a middle school by guanxi this year. So he find out his guanxi net. Boss Liu's guanxi net consists of N people including Boss Liu and the schoolmaster. In this net, two persons who has a guanxi between them can help each other. Because Boss Liu is a big money(In Chinese English, A "big money" means one who has a lot of money) and has little friends, his guanxi net is a naked money guanxi net -- it means that if there is a guanxi between A and B and A helps B, A must get paid. Through his guanxi net, Boss Liu may ask A to help him, then A may ask B for help, and then B may ask C for help ...... If the request finally reaches the schoolmaster, Boss Liu's kid will be accepted by the middle school. Of course, all helpers including the schoolmaster are paid by Boss Liu. 

You hate Boss Liu and you want to undermine Boss Liu's plan. All you can do is to persuade ONE person in Boss Liu's guanxi net to reject any request. This person can be any one, but can't be Boss Liu or the schoolmaster. If you can't make Boss Liu fail, you want Boss Liu to spend as much money as possible. You should figure out that after you have done your best, how much at least must Boss Liu spend to get what he wants. Please note that if you do nothing, Boss Liu will definitely succeed.
Input
There are several test cases. 

For each test case: 

The first line contains two integers N and M. N means that there are N people in Boss Liu's guanxi net. They are numbered from 1 to N. Boss Liu is No. 1 and the schoolmaster is No. N. M means that there are M guanxis in Boss Liu's guanxi net. (3 <=N <= 30, 3 <= M <= 1000) 

Then M lines follow. Each line contains three integers A, B and C, meaning that there is a guanxi between A and B, and if A asks B or B asks A for help, the helper will be paid C RMB by Boss Liu. 

The input ends with N = 0 and M = 0. 

It's guaranteed that Boss Liu's request can reach the schoolmaster if you do not try to undermine his plan.
Output
For each test case, output the minimum money Boss Liu has to spend after you have done your best. If Boss Liu will fail to send his kid to the middle school, print "Inf" instead.
Sample Input
4 5
1 2 3
1 3 7
1 4 50
2 3 4
3 4 2
3 2
1 2 30
2 3 10
0 0
Sample Output
50
Inf
题意:刘老板想通过关系让儿子上学,但是王老板不认识校长,只能通过关系,刘老板每次找一个人就得向这个人付钱,正义的你看到这样的情况很气愤,于是你决定说服一个人且只能说服一个人不帮助刘老板,让刘老板的计划失败或者花的钱更多
这道题想了好久,思路完全错了,应该就是枚举然后选出最大的就可以了!
#include <cstdio>  
#include <cstring>  
#define INF 0x3f3f3f3f  
int map[50][50];  
int dis[50];  
int vis[50];  
int n,m;  
void dj()  
{  
    vis[1]=1;  
    for(int i=1;i<=n;i++)  
    {  
        dis[i]=map[1][i];  
    }  
    for(int i=0;i<n;i++)  
    {  
        int min=INF;  
        int pos=-1;  
        for(int j=1;j<=n;j++)  
        {  
            if(vis[j]==0&&min>dis[j])  
            {  
                min=dis[j];  
                pos=j;  
            }  
        }  
        if(pos==-1)  
        return ;  
        vis[pos]=1;  
        dis[pos]=min;  
        for(int j=1;j<=n;j++)  
        {  
            if(vis[j]==0&&dis[j]>dis[pos]+map[pos][j])  
            {  
                dis[j]=dis[pos]+map[pos][j];  
            }  
        }  
    }  
}  
int main()  
{  
    while(scanf("%d%d",&n,&m)!=EOF)  
    {  
        if(n==0&&m==0)  
        break;  
        for(int i=1;i<=n;i++)  
        {  
            for(int j=i;j<=n;j++)  
            {  
                if(i==j)  
                map[i][j]=0;  
                else  
                {  
                    map[i][j]=INF;  
                    map[j][i]=INF;  
                }  
            }  
        }  
        for(int i=0;i<m;i++)  
        {  
            int x,y,z;  
            scanf("%d%d%d",&x,&y,&z);  
            if(map[x][y]>z)  
            {  
                map[x][y]=z;  
            }  
                map[y][x]=z;  
        }  
        int ans=-1;  
        for(int i=2;i<n;i++) 
        {  
            memset(vis,0,sizeof(vis));  
            vis[i]=1;    
            dj();  
            if(ans<dis[n])//¸üÐÂans   
            {  
                ans=dis[n];  
            }  
        }  
        if(ans==INF)//ÎÞ·¨ÁªÍ¨   
        {  
            printf("Inf\n");  
        }  
        else  
        {  
            printf("%d\n",ans);  
        }  
    }  
    return 0;  
}  


<think>我们正在处理一个关于在macOS上安装Python的请求。用户明确要求参考提供的引用,但需要注意的是,这些引用主要讨论在macOS上安装Python,而用户之前的问题和回答是关于Linux的。因此,我们需要调整内容以适用于macOS。 根据引用,macOS通常预装了Python,但可能是较旧的版本(如Python 2.7或较旧的Python 3版本)。用户可能希望安装最新版本或特定版本的Python。 在macOS上安装Python的常用方法有两种: 1. 使用官方安装程序(从Python官网下载) 2. 使用包管理器(如Homebrew) 由于引用中提到了官方安装程序(引用[2]和[3])以及预装Python的情况(引用[1]和[3]),我们将结合这些信息提供指南。 步骤大纲: 1. 检查预装的Python版本 2. 选择安装方式(官方安装程序或Homebrew) 3. 详细步骤(两种方式) 4. 验证安装 5. 设置默认Python版本(如果需要) 6. 虚拟环境(venv)的使用 注意:在macOS上,系统自带的Python位于/usr/bin/python,我们通常不推荐直接修改系统自带的Python,而是安装新版本并设置路径。 根据引用[3]:“Mac系统自带安装好的Python,可以在终端中执行以下命令查看预安装的Python版本:python --version 或 python3 --version”。同时,引用[2]和[3]提到了自行安装Python的步骤。 我们将提供两种安装方式,并推荐使用Homebrew(因为它更方便管理),但也会提供官方安装程序的方式。 另外,引用[4]提到“轻松掌握MacOS上安装Python的全过程”,我们可以据此给出一个清晰的步骤。 最后,生成相关问题。 由于用户之前的问题是关于Linux的,而现在是macOS,所以内容需要调整,但基本逻辑类似。 注意:在macOS上,我们不需要像Linux那样安装编译依赖,因为官方安装程序已经包含所需内容,或者使用Homebrew会自动处理依赖。 下面开始编写回答:</think>### macOS 安装 Python 完整指南 在 macOS 上安装 Python 有两种推荐方式:使用官方安装程序或通过 Homebrew 包管理器。以下是详细步骤: #### 方法一:使用官方安装程序(推荐初学者) 1. **检查预装 Python 版本** macOS 自带 Python 2.7 或旧版 Python 3: ```bash python --version # 检查 Python 2.x python3 --version # 检查 Python 3.x ``` 2. **下载最新安装包** 访问 [Python 官网](https://www.python.org/downloads/macos/) 下载最新 .pkg 文件(如 Python 3.12.x) 3. **运行安装程序** - 双击下载的 .pkg 文件 - 按向导完成安装(默认安装到 `/Library/Frameworks/Python.framework/`) - 勾选 **"Add Python to PATH"** 选项[^1][^3] 4. **验证安装** ```bash python3 --version # 应显示类似: Python 3.12.1 ``` #### 方法二:使用 Homebrew(推荐开发者) 1. **安装 Homebrew** ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 2. **通过 Homebrew 安装 Python** ```bash brew update brew install python@3.12 # 安装指定版本 ``` 3. **配置环境变量** ```bash echo 'export PATH="/usr/local/opt/python@3.12/bin:$PATH"' >> ~/.zshrc source ~/.zshrc ``` #### 安装后配置 1. **设置默认 Python 版本** ```bash # 查看所有安装路径 ls -l /usr/local/bin/python* # 创建符号链接(示例) sudo ln -s -f /usr/local/bin/python3.12 /usr/local/bin/python3 ``` 2. **安装虚拟环境** ```bash python3 -m pip install virtualenv # 创建虚拟环境 python3 -m virtualenv myenv source myenv/bin/activate ``` #### 验证 SSL 支持(关键步骤) ```bash python3 -c "import ssl; print(ssl.OPENSSL_VERSION)" # 应显示 OpenSSL 版本号[^3] ``` #### 卸载 Python ```bash # 官方安装版 sudo rm -rf /Library/Frameworks/Python.framework # Homebrew 版 brew uninstall python@3.12 brew cleanup ``` > ⚠️ **重要提示** > - 不要删除系统自带的 `/usr/bin/python3`,以免影响系统功能[^3] > - 推荐使用虚拟环境隔离项目依赖 > - 定期更新:`brew upgrade python@3.12` ### 常见问题解决 - **"Command not found" 错误**:检查 `~/.zshrc` 中的 PATH 配置 - **SSL 模块缺失**:重装时确保勾选 "Install certificates" 选项 - **多版本冲突**:使用 `pyenv` 工具管理多版本 通过以上步骤,您可以在 macOS 上安全地安装和管理 Python 环境[^1][^2][^3][^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值