a1111 Online Map(dijkstra + dfs)

1111 Online Map (30 分)

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where and are the indices (from 0 to V1V2N−1) of the two ends of the street; is 1 if the street is one-way from to , or 0 if not; is the length of the street; and is the time taken to pass the street.one-wayV1V2lengthtime

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance in the format:D

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time :T

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

题意:给出每个节点的联通关系,输出两条路线:1、最短的路线,如果有多条最短的,则输出最快的。2、最快的路线,如果有多条,则输出最节点最少的路线。

思路:两次 dijkstra遍历,第一次选出最短的路线,第二次选出最快的路线;然后两次dfs,第一次选出最快的,第二次选出节点最少的路线。

#include <iostream>
#include "vector"
using namespace std;
const int maxx=510,INF=9999999;
int g[maxx][maxx],times[maxx][maxx],d[maxx],d1[maxx];
bool vis[maxx];
int n,m,s,des,shortfast=INF,fastfast=INF,mininter=INF;
vector<int> shortpath,pre[maxx],tempath,fastpath;
//选出最短的路线
void dfs(int st){
    fill(d,d+maxx,INF);
    d[st]=0;
    for (int i = 0; i < n; ++i) {
        int u=-1,MIN=INF;
        for (int j = 0; j < n; ++j) {
            if (!vis[j] && d[j] < MIN){
                u=j;
                MIN=d[j];
            }
        }
        if (u==-1) return;
        vis[u]= true;
        for (int j = 0; j <n ; ++j) {
            if (!vis[j] && g[u][j] < INF){
                if (d[u] + g[u][j] < d[j]){
                    d[j] = d[u]+g[u][j];
                    pre[j].clear();
                    pre[j].push_back(u);
                } else if (d[u]+g[u][j] == d[j]){
                    pre[j].push_back(u);
                }
            }
        }
    }
}
//选出耗时最短的
void dfs1(int st){
    fill(d1,d1+maxx,INF);
    d1[st]=0;
    for (int i = 0; i < n; ++i) {
        int u=-1,MIN=INF;
        for (int j = 0; j < n; ++j) {
            if (!vis[j] && d1[j] < MIN){
                u=j;
                MIN=d1[j];
            }
        }
        if (u==-1) return;
        vis[u]= true;
        for (int j = 0; j <n ; ++j) {
            if (!vis[j] && times[u][j] < INF){
                if (d1[u] + times[u][j] < d1[j]){
                    d1[j] = d1[u]+times[u][j];
                    pre[j].clear();
                    pre[j].push_back(u);
                } else if (d1[u]+times[u][j] == d1[j]){
                    pre[j].push_back(u);
                }
            }
        }
    }
}
//选出最短的路线中耗时最少的
void dfstravel(int v){
    if (v==s){
        tempath.push_back(v);
        int cost=-1;
        for (int i = tempath.size()-1; i > 0; --i) {
            int now = tempath[i],next=tempath[i-1];
            cost += times[now][next];
        }
        if (cost < shortfast){
            shortfast = cost;
            shortpath = tempath;
        }
        tempath.pop_back();
        return;
    }
    tempath.push_back(v);
    for (int i = 0; i < pre[v].size(); ++i) {
        dfstravel(pre[v][i]);
    }
    tempath.pop_back();
}
//耗时最短的选出节点最少
void dfstravel1(int v){
    if (v==s){
        tempath.push_back(v);
        int len=tempath.size();
//        for (int i = tempath.size()-1; i > 0; --i) {
//            int now = tempath[i],next=tempath[i-1];
//            cost += times[now][next];
//        }
        if (len < mininter){
            mininter = len;
            fastpath = tempath;
        }
        tempath.pop_back();
        return;
    }
    tempath.push_back(v);
    for (int i = 0; i < pre[v].size(); ++i) {
        dfstravel1(pre[v][i]);
    }
    tempath.pop_back();
}
int main() {
    cin>>n>>m;
    fill(g[0],g[0]+maxx*maxx,INF);
    fill(times[0],times[0]+maxx*maxx,INF);
    for (int i = 0; i < m; ++i) {
        int v1,v2,flag,len,time;
        cin>>v1>>v2>>flag>>len>>time;
        g[v1][v2]=len;
        times[v1][v2]=time;
        if (flag==0){
            g[v2][v1]=len;
            times[v2][v1]=time;
        }
    }
    cin>>s>>des;
    dfs(s);
    dfstravel(des);
    fill(vis,vis+maxx, false);
    dfs1(s);
    dfstravel1(des);
    if (shortpath != fastpath){
        printf("Distance = %d: ",d[des]);
        for (int i = shortpath.size()-1; i >=0 ; --i) {
            if (i == shortpath.size()-1) printf("%d",shortpath[i]);
            else printf(" -> %d",shortpath[i]);
        }
        printf("\n");
        printf("Time = %d: ",d1[des]);
        for (int i = fastpath.size()-1; i >=0 ; --i) {
            if (i == fastpath.size()-1) printf("%d",fastpath[i]);
            else printf(" -> %d",fastpath[i]);
        }
    } else {
        printf("Distance = %d; Time = %d: ",d[des],d1[des]);
        for (int i = fastpath.size()-1; i >=0 ; --i) {
            if (i == fastpath.size()-1) printf("%d",fastpath[i]);
            else printf(" -> %d",fastpath[i]);
        }
    }
}

<think>好的,我需要帮助用户从字符串“(a1111,b222)”中提取出a1111和b222这两个部分。首先,我需要理解这个字符串的结构。看起来这是一个由括号包围的、用逗号分隔的两个子字符串的组合。用户的目的是要提取这两个子字符串,去除括号和逗号,只保留里面的内容。 首先,我需要确认这个字符串的格式是否固定。例如,是否总是以左括号开始,然后是第一个值,接着是逗号,然后是第二个值,最后以右括号结束。如果是的话,可以考虑使用字符串分割的方法来处理。具体来说,可以去掉首尾的括号,然后将剩下的字符串按逗号分割成两部分,分别得到a1111和b222。 接下来,我需要考虑可能的异常情况,例如字符串中没有括号,或者逗号的位置不固定,或者有其他额外的字符。不过根据用户提供的例子“(a1111,b222)”,结构看起来比较规范,所以可能不需要处理太复杂的情况。 然后,我需要确定使用哪种方法来实现这个提取。常见的方法包括使用字符串的split方法,或者使用正则表达式来匹配所需的部分。例如,正则表达式可以更灵活地处理不同的情况,比如括号内的内容可能有空格,或者其他分隔符。不过在这个例子中,可能不需要那么复杂的正则表达式。 具体来说,处理步骤可能如下: 1. 去除字符串两端的括号。 2. 将剩下的字符串按逗号分割成两个部分。 3. 去除可能存在的空格或其他多余字符,得到最终的a1111和b222。 不过在这个例子中,用户给出的字符串“(a1111,b222)”内部没有空格,所以直接分割即可。例如,在Python中,可以这样实现: - 使用strip()方法去掉首尾的括号。 - 使用split(',')将字符串分成两部分。 测试这个逻辑,如果输入是“(a1111,b222)”,处理后应该得到['a1111', 'b222'],也就是两个需要的部分。 另外,需要考虑是否有可能有转义字符或者其他特殊符号,但根据用户提供的例子,暂时不需要考虑这些情况。如果用户有其他格式的字符串,可能需要调整方法,但目前按照给出的例子处理即可。 总结来说,步骤是: 1. 去掉首尾的括号。 2. 按逗号分割成两个子字符串。 3. 分别取出这两个子字符串作为结果。 这样应该能正确提取出a1111和b222。</think>要提取字符串$(a1111,b222)$中的`a1111`和`b222`,可以按照以下步骤操作: --- ### **步骤 1:去除首尾的括号** 原字符串为:$(a1111,b222)$ 使用字符串截断方法去掉首尾的`(`和`)`,得到中间部分: $$ \text{中间内容} = a1111,b222 $$ --- ### **步骤 2:按逗号分割字符串** 将中间内容按逗号`,`分割成两部分: $$ \text{分割结果} = [\text{a1111}, \text{b222}] $$ --- ### **步骤 3:提取目标值** 直接取分割后的第一个和第二个元素: - **第一个值**:$a1111$ - **第二个值**:$b222$ --- ### **代码示例(Python)** ```python s = "(a1111,b222)" # 去括号后按逗号分割 parts = s.strip("()").split(",") a = parts[0] # 输出 a1111 b = parts[1] # 输出 b222 ``` --- ### **其他注意事项** 1. 若字符串中包含空格(如$(a 1111, b 222)$),需先去除空格: ```python parts = s.strip("()").replace(" ", "").split(",") ``` 2. 若需严格校验格式,可使用正则表达式匹配: ```python import re match = re.match(r"\((\w+),(\w+)\)", s) a, b = match.groups() # 输出 a1111 和 b222 ``` --- 通过以上步骤,即可准确提取字符串中的`a1111`和`b222`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值