最短路径:Shortest Reach

        Given an undirected graph consisting of  nodes (labelled 1 to N) where a specific given node  represents the start position and an edge between any two nodes is of length  units in the graph. It is required to calculate the shortest distance from start position (Node S) to all of the other nodes in the graph.

     Note 1: If a node is unreachable , the distance is assumed as 
     Note 2: The length of each edge in the graph is  units.

    Input Format

   The first line contains , denoting the number of test cases. 
   First line of each test case has two integers , denoting the number of nodes in the graph and , denoting the number of edges in the graph. 
The next  lines each consist of two space separated integers , where  and  denote the two nodes between which the edge exists. 
The last line of a testcase has an integer , denoting the starting position.

Constraints 
 Output Format

For each of  test cases, print a single line consisting of  space-separated integers, denoting the shortest distances of the N-1 nodes from starting position . This will be done for all nodes same as in the order of input 1 to N.

For unreachable nodes, print .

Sample Input

2
4 2
1 2
1 3
1
3 1
2 3
2

Sample Output

6 6 -1
-1 6

Explanation

For test cases 1:

The graph given in the test case is shown as :

Graph

S denotes the node 1 in the test case and B,C and D denote 2,3 and 4. Since S is the starting node and the shortest distances from it are (1 edge, 1 edge, Infinity) to the nodes B,C and D (2,3 and 4) respectively.

Node D is unreachable, hence -1 is printed (not Infinity).

For test cases 2: There are only one edge (2, 3) in a graph with 3 nodes, so node 1 is unreachable from node 2, and node 3 has one edge from node 2, each edge has the length of 6 units. So we output -1 6.

解题方法一:弗洛伊德算法,结果超时:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
unsigned int MAXVALUE = 5000001;
#define MAX 1001
unsigned int map[MAX][MAX];
unsigned int N,M;
unsigned int dist[MAX];
unsigned int T;
unsigned int visited[MAX];
int queue[MAX*MAX];
int main() 
{
    cin >> T;
    for(int i = 1;i<=T;i++)
    {
       cin>>N>>M;
        for(int index =1; index <=N;index++)
        {
            for(int x = 1;x<=N;x++)
            {
              map[index][x] = MAXVALUE;    
            }
            visited[index] = 0;
            dist[index] = 0;
        }
       for(int j =1; j<= M;j++)
       {
           int x,y;
           cin>>x>>y;
           map[x][y] = 1;
           map[y][x] = 1;   
       }
       int S;
       cin>>S;
       int head ,tear = 0;
       head = 0;
       visited[S] = 1;
       queue[tear++] = S;
       dist[S] = 0;
       while(head != tear)
       {
          int cur = queue[head];
          for(int i = 1 ; i<=N;i++)
          {
              if(map[cur][i] == 1 && visited[i] == 0)
              {
                   dist[i] = dist[cur] + 6;
                   visited[i] = 1;
                   queue[tear++] = i;
              }      
          }
          head++;
        }
       for(int index0 = 1; index0 <=N;index0++)
       {
          if( index0 !=S)
          {
               if(dist[index0] != 0)
              {
                 cout<<dist[index0]<<" ";
              }
              else
              {
              cout<<"-1 ";
              }
          }
         
       }
       cout<<endl;
    }/* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

迪杰斯特拉:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
unsigned int MAXVALUE = 5000001;
#define MAX 1001
unsigned int map[MAX][MAX];
unsigned int N,M;
unsigned int dist[MAX];
unsigned int T;
unsigned int visited[MAX];
int queue[MAX*MAX];
int main() 
{
    cin >> T;
    for(int i = 1;i<=T;i++)
    {
       cin>>N>>M;
        for(int index =1; index <=N;index++)
        {
            for(int x = 1;x<=N;x++)
            {
              map[index][x] = MAXVALUE;    
            }
            visited[index] = 0;
            dist[index] = 0;
        }
       for(int j =1; j<= M;j++)
       {
           int x,y;
           cin>>x>>y;
           map[x][y] = 1;
           map[y][x] = 1;   
       }
       int S;
       cin>>S;
       int head ,tear = 0;
       head = 0;
       visited[S] = 1;
       queue[tear++] = S;
       dist[S] = 0;
       while(head != tear)
       {
          int cur = queue[head];
          for(int i = 1 ; i<=N;i++)
          {
              if(map[cur][i] == 1 && visited[i] == 0)
              {
                   dist[i] = dist[cur] + 6;
                   visited[i] = 1;
                   queue[tear++] = i;
              }      
          }
          head++;
        }
       for(int index0 = 1; index0 <=N;index0++)
       {
          if( index0 !=S)
          {
               if(dist[index0] != 0)
              {
                 cout<<dist[index0]<<" ";
              }
              else
              {
              cout<<"-1 ";
              }
          }
         
       }
       cout<<endl;
    }/* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

BFS:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
unsigned int MAXVALUE = 5000001;
#define MAX 1001
unsigned int map[MAX][MAX];
unsigned int N,M;
unsigned int dist[MAX];
unsigned int T;
unsigned int visited[MAX];
int queue[MAX*MAX];
int main() 
{
    cin >> T;
    for(int i = 1;i<=T;i++)
    {
       cin>>N>>M;
        for(int index =1; index <=N;index++)
        {
            for(int x = 1;x<=N;x++)
            {
              map[index][x] = MAXVALUE;    
            }
            visited[index] = 0;
            dist[index] = 0;
        }
       for(int j =1; j<= M;j++)
       {
           int x,y;
           cin>>x>>y;
           map[x][y] = 1;
           map[y][x] = 1;   
       }
       int S;
       cin>>S;
       int head ,tear = 0;
       head = 0;
       visited[S] = 1;
       queue[tear++] = S;
       dist[S] = 0;
       while(head != tear)
       {
          int cur = queue[head];
          for(int i = 1 ; i<=N;i++)
          {
              if(map[cur][i] == 1 && visited[i] == 0)
              {
                   dist[i] = dist[cur] + 6;
                   visited[i] = 1;
                   queue[tear++] = i;
              }      
          }
          head++;
        }
       for(int index0 = 1; index0 <=N;index0++)
       {
          if( index0 !=S)
          {
               if(dist[index0] != 0)
              {
                 cout<<dist[index0]<<" ";
              }
              else
              {
              cout<<"-1 ";
              }
          }
         
       }
       cout<<endl;
    }/* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}


内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
### 动态规划算法解决多段图最短路径问题 #### 定义与背景 在处理最短路径问题时,动态规划提供了一种高效的方法来找到从起点到终点的最佳路径。对于多段图而言,这类问题是典型的动态规划应用场景之一。多段图是指节点被划分为多个阶段或层次的有向加权图,每一段内的任意两个结点之间不存在边连接[^1]。 #### 解决方案概述 为了应用动态规划方法求解此类问题,通常会定义状态转移方程以及初始化条件。具体来说: - **状态表示**:设`d[i][j]`代表从第i段到达第j个节点所需的最小花费。 - **初始设置**:如果存在直接通往第二层某些节点的道路,则这些道路的成本即为对应的`d[1][j]`值;其他情况下,默认赋极大值(无穷大),因为尚未访问过那些位置。 - **递推关系**:对于每一个可能的目标节点k,在其前驱集合{p}内遍历所有可选路径,并更新当前最优解:`d[i][k]=min(d[i−1][p]+cost(p,k))`,这里`cost(p, k)`指的是从前一阶段中的某个特定点移动至下一阶段指定目的地所需支付的价格标签[^2]。 #### Python代码实现示例 下面是基于上述原理编写的Python程序片段用于演示如何运用动态规划技术计算给定多段图中最优行走路线: ```python import numpy as np def shortest_path_multistage_graph(graph_matrix): num_stages = len(graph_matrix) # 获取总共有多少级/列 dp_table = [[float('inf')]*len(row) for row in graph_matrix] # 初始化起始阶段成本矩阵的第一行(假设只有第一个元素有效) dp_table[0][0] = 0 # 填充DP表 for stage_index in range(1, num_stages): # 遍历每一级 current_stage_nodes_count = len(graph_matrix[stage_index]) for node_in_current_stage in range(current_stage_nodes_count): prev_stage_node_costs = dp_table[stage_index - 1] min_cost_to_reach_here = float('inf') for previous_node_idx, cost_from_prev in enumerate(prev_stage_node_costs): edge_weight = graph_matrix[previous_node_idx][node_in_current_stage] if not np.isnan(edge_weight): # 如果两者间确实有一条边相连 total_temporary_cost = cost_from_prev + edge_weight if total_temporary_cost < min_cost_to_reach_here: min_cost_to_reach_here = total_temporary_cost dp_table[stage_index][node_in_current_stage] = min_cost_to_reach_here final_minimal_cost = min(dp_table[-1]) # 找到最后一个阶段里最低的那个数值作为整体的结果返回 return final_minimal_cost ``` 该函数接受一个多维列表形式输入参数graph_matrix,其中每个子列表对应于不同级别的邻接权重数组。注意这里的NaN用来标记两节点间无连通情况下的特殊情形[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值