uva11354 - Bond

本文介绍了一个算法问题,即如何为James Bond找到从一个城市到另一个城市的最不危险路径,以避免被警察发现。通过构建图论模型,并使用MST、倍增法等技术,实现了高效的路径查找。

B

NEXT Generation Contest 4

Time Limit – 8 secs

Bond

 

Once again, James Bond is on his way to saving the world. Bond's latest mission requires him to travel between several pairs of cities in a certain country.

 

The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads. Bond is going to steal a vehicle, and drive along the roads from city s to city t. The country's police will be patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the police.

 

More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is defined as the maximum dangerousness of any road on this path.

 

Now, it's your job to help Bond succeed in saving the world by finding the least dangerous paths for his mission.

 

 

Input

There will be at most 5 cases in the input file.

The first line of each case contains two integers NM (2 ≤ N≤ 50000, 1≤ M ≤ 100000) – number of cities and roads. The next M lines describe the roads. The i-th of these lines contains three integers: xiyidi (1 ≤ xiyi ≤ N, 0 ≤ di ≤ 109) - the numbers of the cities connected by the ith road and its dangerousness.

 

Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by Q lines, the i-th of which contains two integers si and ti (1 ≤ siti  ≤ Nsi != ti).

 

Consecutive input sets are separated by a blank line.

 

Output

For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between cities si and ti. Consecutive output blocks are separated by a blank line.

 

The input file will be such that there will always be at least one valid path.

 

Sample Input

Output for Sample Input

4 5

1 2 10

1 3 20

1 4 100

2 4 30

3 4 10

2

1 4

4 1

 

2 1

1 2 100

1

1 2

20

20

 

100

 

ProblemSetter: Ivan Krasilnikov 




求最小瓶颈路中的最大值,由于数据范围大,

利用倍增法求最大值。

先求出MST,将无根树转化为有根树,进行LCA的预处理,

在求LCA的过程中顺便将s,t两点到公共祖先的路径上的最大边权更新

这样就可以在线处理多组s,t 。



#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

#define N 50050
#define M 100010

const int inf=0x3f3f3f3f;

struct Edge
{
    int x,y,w;
};

struct data
{
    int y,w;
};

Edge e[M];
int fa[N],dep[N],cost[N],P[N];
int anc[N][20],maxcost[N][20];
bool vis[N];
vector <data> G[N];
int n,m,Q;

bool cmp(Edge a, Edge b)
{
    return a.w<b.w;
}

int up(int x)
{
    return fa[x]==x? x: fa[x]=up(fa[x]);
}

void dfs(int u)
{
    vis[u]=1;
    for (int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i].y,w=G[u][i].w;
        if (!vis[v])
        {
            P[v]=u;
            cost[v]=w;
            dep[v]=dep[u]+1;
            dfs( v );
        }
    }
}

void Kruskal()
{
    for (int i=1; i<=n; i++)
        G[i].clear();
    for (int i=1; i<=n; i++)
        fa[i]=i;
    sort(e+1,e+1+m,cmp);
    int cnt=0;
    for (int i=1; i<=m; i++)
    {
        int f1=up(e[i].x);
        int f2=up(e[i].y);
        if (f1!=f2)
        {
            cnt++;
            fa[f1]=f2;
            G[e[i].x].push_back( (data){e[i].y,e[i].w} );
            G[e[i].y].push_back( (data){e[i].x,e[i].w} );
            if (cnt==n-1)  break;
        }
    }

    memset(vis,0,sizeof(vis));
    P[1]=-1;
    cost[1]=dep[1]=0;
    dfs(1);
}

void init()
{
    for (int i=1; i<=n; i++)
    {
        anc[i][0]=P[i];
        maxcost[i][0]=cost[i];
        for (int j=1; (1<<j)<n; j++)
            anc[i][j]=-1;
    }
    for (int j=1; (1<<j) < n; j++)
        for (int i=1; i <= n; i++)
        {
            if (anc[i][j-1]!=-1)
            {
                int p=anc[i][j-1];
                anc[i][j]=anc[ p ][j-1];
                maxcost[i][j]=max( maxcost[i][j-1],maxcost[p][j-1]);
            }
        }
}

int query(int u,int v)
{
    if (dep[u]<dep[v]) swap(u,v);
    int l=0;
    while (1<<(l+1)<=dep[u])
        l++;
    int ans=-inf;
    for (int i=l; i>=0; i--)
        if (dep[u]-(1<<i)>=dep[v])
        {
            ans=max(ans, maxcost[u][i]);
            u=anc[u][i];
        }
    if (u==v)  return ans;
    for (int i=l; i>=0; i--)
    {
        if (anc[u][i]!=-1&&anc[u][i]!=anc[v][i])
        {
            ans=max( maxcost[u][i],ans); u=anc[u][i];
            ans=max( maxcost[v][i],ans); v=anc[v][i];
        }
    }
    ans=max(ans,cost[u]);
    ans=max(ans,cost[v]);
    return ans;
}

int main()
{
    int flag=0;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        if (flag++) printf("\n");
        for (int i=1; i<=m; i++)
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
        Kruskal();
        init();
        scanf("%d",&Q);
        for (int i=1; i<=Q; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            printf("%d\n",query(u,v));
        }

    }
    return 0;
}


<think>嗯,用户想了解关于NetworkManager绑定设备(nm-bond)的配置或问题。首先,我需要回忆NetworkManager中配置bonding的相关知识。记得bonding是将多个网络接口聚合为一个逻辑接口,提供冗余或提高带宽。NetworkManager支持通过nmcli或nmtui配置,可能需要用到不同的模式,比如balance-rr(mode 0)或者active-backup(mode 1)等。 然后,用户提供的引用里有几个相关的命令和配置片段,比如引用3和4提到了使用bonding.py脚本和modprobe命令加载bonding模块,设置不同的模式。这可能涉及到内核模块的加载和参数设置。在NetworkManager中,可能需要用nmcli来创建bond接口,并指定从属接口和模式。 接下来,我需要确保回答符合系统级指令的要求,比如行内数学公式用$...$,独立公式用$$...$$,虽然这里可能用不上数学公式,但要注意其他格式要求。使用中文回答,结构清晰,分步骤说明配置方法,并生成相关问题。 可能的问题点包括:如何创建bond设备,如何选择模式,如何设置IP地址,以及常见问题如接口不启动或模式不生效。需要结合引用中的信息,比如引用4提到mode=0和mode=1,miimon参数,这些在配置时可能需要用到。此外,引用2中的启用或禁用多播可能在某些情况下相关。 还要注意用户提到的nm-bond,即使用NetworkManager而不是传统的配置文件(如/etc/network/interfaces),所以重点放在nmcli命令上。例如,使用nmcli创建bond,添加从属接口,设置模式参数等。同时,需要指出常见问题排查步骤,比如检查日志,确认模块加载,从属接口状态等。 最后,生成的相关问题需要围绕bond配置的扩展,比如不同模式的区别、负载均衡配置、与VLAN的结合使用,以及故障排除方法。确保每个步骤都清晰,并且引用正确,比如引用中的配置示例可能需要标注引用标记。</think>### NetworkManager Bond设备配置指南 #### 一、基础配置步骤(使用nmcli) 1. **创建Bond接口** ```bash nmcli con add type bond con-name bond0 ifname bond0 bond.options "mode=802.3ad,miimon=100" ipv4.method manual ipv4.addresses 192.168.1.11/24 ``` - `mode=802.3ad`对应IEEE 802.3ad动态链聚合[^3] - `miimon=100`表示每100ms检查链状态[^4] 2. **添加从属接口** ```bash nmcli con add type bond-slave ifname eth0 master bond0 nmcli con add type bond-slave ifname eth1 master bond0 ``` 3. **激活配置** ```bash nmcli con up bond0 ``` #### 二、关键参数说明 | 参数 | 作用 | 示例值 | |------|------|--------| | mode | 绑定模式 | 802.3ad/active-backup | | primary | 主接口 | eth0[^4] | | miimon | 链检测间隔 | 100ms | #### 三、常见问题排查 1. **接口未启动** ```bash journalctl -u NetworkManager -f # 查看实时日志 cat /proc/net/bonding/bond0 # 验证bond状态 ``` 2. **模式不生效** - 确认交换机配置匹配(如802.3ad需要LACP支持)[^1] - 检查内核模块加载:`lsmod | grep bonding` 3. **IP分配失败** ```bash nmcli device show bond0 # 检查IP配置 ip link set bond0 multicast on # 启用多播[^2] ``` #### 四、高级配置示例 ```bash nmcli con add type bond con-name bond1 ifname bond1 \ bond.options "mode=active-backup,primary=eth2,miimon=50" \ ipv4.gateway 192.168.1.1 ``` 此配置实现主备模式,eth2作为主接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值