find the longest of the shortest

本文介绍了一种解决特定图论问题的方法:当一条边失效时如何找到从起点到终点的最坏情况下的最短路径。通过使用Dijkstra算法,并在移除关键边后重新计算最短路径来解决问题。

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

https://vjudge.net/contest/237668#problem/C

题目的意思就是Marica去Mirko的城市找他,走最短路,但有一条路不能走了,求它的最坏情况下的最短路径

思路就是先求出最短路径,然后假设最短路径中的某一条边不能走

#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,u,v,w;
int a[1020][1020];
int dis[1020];
int vis[1020];
int pre[1010];
void dijkstra(int k)
{
    for(int i=1; i<=n; i++)
    {
        dis[i]=a[1][i];
        if(k)pre[i]=1;//记录最短路经过的城市编号
    }
    memset(vis,0,sizeof(vis));
    vis[1]=1;
    for(int i=1; i<=n-1; i++)
    {
        int mmin=inf;
        int u=-1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<mmin)
            {
                mmin=dis[j];
                u=j;
            }
        }
        if(u==-1)
            break;
        vis[u]=1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]>dis[u]+a[u][j])
            {
                dis[j]=dis[u]+a[u][j];
                if(k)pre[j]=u;
            }
        }
    }
}
int main()
{

    while(~scanf("%d%d",&n,&m))
    {
        memset(a,inf,sizeof(a));
        for(int i=1; i<=n; i++)
            a[i][i]=0;
        int u,v,w;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            a[u][v]=w;
            a[v][u]=w;
        }
        dijkstra(1);
        pre[1]=-1;
        int maxn=0;
        int r=n;
        for(int i=pre[n];i!=-1;i=pre[i])
        {
            int t=a[i][r];
            a[i][r]=a[r][i]=inf;//把一条路堵死
            dijkstra(0);//求最短路
            maxn=max(maxn,dis[n]);
            a[i][r]=a[r][i]=t;
            r=i;
        }
        printf("%d\n",maxn);
    }
}

,再求最短路,取这些最短路的最大值即可。

 

### Python中查找字符串中最长和最短单词 在Python中,为了找到字符串中的最长和最短单词,通常会先将该字符串分割成单独的单词列表。这一步骤可通过`split()`方法完成[^1]。 对于寻找最短单词的操作,在遍历这个列表的同时维护一个变量来跟踪遇到的最短单词及其长度。一旦完成了整个列表的迭代,则可以获得最终的结果。下面是一个简单的实现方式: ```python def find_shortest_word(sentence): words = sentence.split() if not words: return None shortest_word = min(words, key=len) return shortest_word sentence = "The quick brown fox jumps over the lazy dog" shortestWord = find_shortest_word(sentence) if shortestWord: print(f"The shortest word(s) in the list is: {shortestWord}.") else: print("No words found.") ``` 同样地,要找出最长的单词也可以采用相似的方式,只是这次是在循环过程中记录下最长的那个词以及它的长度。这里给出一段用于求解最长单词的代码片段: ```python def find_longest_word(sentence): words = sentence.split() if not words: return None longest_words = [word for word in words if len(word) == max(len(w) for w in words)] return longest_words sentence = "The quick brown fox jumps over the lazy dog" longestWords = find_longest_word(sentence) if longestWords: print("The longest words are:") for lw in longestWords: print(lw) print(f"The length of the longest word(s) is: {len(longestWords[0])}") print(f"There are {len(longestWords)} longest word(s)") else: print("No words found.") ``` 上述两段代码分别展示了如何在一个句子内定位到最短与最长的词语,并打印相应的信息[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值