最短路:HDU1688 & HDU3191

博客围绕两个路径计算问题展开。一是旅游路线规划,要找出从起始城市到终点城市的最短及比最短长一个单位距离的路线数量;二是软件工程师上班路线问题,需找出次短路径的长度和数量。都可在求最短路时求次短路,并记录路径条数。

 

Sightseeing

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1620    Accepted Submission(s): 702


 

Problem Description

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.



For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.

Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

 

 

 

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.

M lines, each with three integers A, B and L, separated by single spaces, with 1 ≤ A, B ≤ N, A ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.

The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.

One line with two integers S and F, separated by a single space, with 1 ≤ S, F ≤ N and S ≠ F: the starting city and the final city of the route.

There will be at least one route from S to F.

 

 

 

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 10^9 = 1,000,000,000.
 

 

 

Sample Input


 

2 5 8 1 2 3 1 3 2 1 4 5 2 3 1 2 5 3 3 4 2 3 5 4 4 5 3 1 5 5 6 2 3 1 3 2 1 3 1 10 4 5 2 5 2 7 5 2 7 4 1

 

 

Sample Output


 

3 2

 

菜是原罪,第一次写这种类型的题目QAQ。用dijkstra求最短路的同时求次短路,同时用cnt数组记录最短路及次短路的条数。需要注意的是可能从 u 到 v 有多条路,所以在这里只能用vector来存边,以及在dijkstra中最外层循环是2*n次而非n次。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 1005
int dis[N][2]; 
int cnt[N][2];
bool vis[N][2];
int n, m;
struct Edge{
	int v;
	int c;
	Edge(int _v = 0, int _c = 0): v(_v), c(_c) {};
};
vector<Edge> w[N];
void addedge(int u, int v, int c)
{
	w[u].push_back(Edge(v, c));
}
void dijkstra(int s, int e)
{
	memset(dis, INF, sizeof(dis));
	memset(vis, false, sizeof(vis));
	dis[s][0] = 0;
	cnt[s][0] = 1;
	for(int i = 1; i <= 2 * n; i++)
	{
		int minn = INF;
		int k = -1;
		int flag;
		for(int j = 1; j <= n; j++)
		{
			if(minn > dis[j][0] && !vis[j][0])
			{
				minn = dis[j][0];
				k = j;
				flag = 0;
			}
			else if(minn > dis[j][1] && !vis[j][1])
			{
				minn = dis[j][1];
				k = j;
				flag = 1;
			}
		}
		if(k == -1) break;
		vis[k][flag] = true;
		for(int j = 0; j < w[k].size(); j++)
		{
			int v = w[k][j].v;
			if(dis[v][0] > minn + w[k][j].c)
			{
				dis[v][1] = dis[v][0];
				cnt[v][1] = cnt[v][0];
				dis[v][0] = minn + w[k][j].c;
				cnt[v][0] = cnt[k][flag];
			}
			else if(dis[v][0] == minn + w[k][j].c)
			{
				cnt[v][0] += cnt[k][flag];
			}
			else if(dis[v][1] == minn + w[k][j].c)
			{
				cnt[v][1] += cnt[k][flag];
			}
			else if(dis[v][1] > minn + w[k][j].c)
			{
				dis[v][1] = minn + w[k][j].c;
				cnt[v][1] = cnt[k][flag];
			}
		}
	}
	if(dis[e][0] + 1 == dis[e][1]) printf("%d\n", cnt[e][0] + cnt[e][1]);
	else printf("%d\n", cnt[e][0]);
}
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; i++) w[i].clear();
		for(int i = 0; i < m; i++)
		{
			int u, v, c;
			scanf("%d%d%d", &u, &v, &c);
			addedge(u, v, c);
		}
		int s, e;
		scanf("%d%d", &s, &e);
		dijkstra(s, e);
	}
	return 0;
}

 

 

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2067    Accepted Submission(s): 735


 

Problem Description

  oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late…Time goes by, he find that he should have some changes as you could see, always riding with the same path is boring.
  One day, oooccc1 got an idea! Why could I take another path? Tired at all the tasks he got, he got no time to carry it out. As a best friend of his, you’re going to help him!
  Since oooccc1 is now getting up earlier, he is glad to take those paths, which are a little longer than the shortest one. To be precisely, you are going to find all the second shortest paths.
  You would be given a directed graph G, together with the start point S which stands for oooccc’1 his house and target point E presents his office. And there is no cycle in the graph. Your task is to tell him how long are these paths and how many there are.

 

 

Input

There are some cases. Proceed till the end of file.
The first line of each case is three integers N, M, S, E (3 <= N <= 50, 0 <= S , E <N)
N stands for the nodes in that graph, M stands for the number of edges, S stands for the start point, and E stands for the end point.
Then M lines follows to describe the edges: x y w. x stands for the start point, and y stands for another point, w stands for the length between x and y.
All the nodes are marked from 0 to N-1.

 

 

Output

For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.

 

 

Sample Input


 

3 3 0 2 0 2 5 0 1 4 1 2 2

 

 

Sample Output


 

6 1

跟上面那题一个套路

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 55
int dis[N][2]; 
int cnt[N][2];
bool vis[N][2];
int n, m;
struct Edge{
	int v;
	int c;
	Edge(int _v = 0, int _c = 0): v(_v), c(_c) {};
};
vector<Edge> w[N];
void addedge(int u, int v, int c)
{
	w[u].push_back(Edge(v, c));
}
void dijkstra(int s, int e)
{
	memset(dis, INF, sizeof(dis));
	memset(vis, false, sizeof(vis));
	dis[s][0] = 0;
	cnt[s][0] = 1;
	for(int i = 1; i <= 2 * n; i++)
	{
		int minn = INF;
		int k = -1;
		int flag;
		for(int j = 0; j < n; j++)
		{
			if(minn > dis[j][0] && !vis[j][0])
			{
				minn = dis[j][0];
				k = j;
				flag = 0;
			}
			else if(minn > dis[j][1] && !vis[j][1])
			{
				minn = dis[j][1];
				k = j;
				flag = 1;
			}
		}
		if(k == -1) break;
		vis[k][flag] = true;
		for(int j = 0; j < w[k].size(); j++)
		{
			int v = w[k][j].v;
			if(dis[v][0] > minn + w[k][j].c)
			{
				dis[v][1] = dis[v][0];
				cnt[v][1] = cnt[v][0];
				dis[v][0] = minn + w[k][j].c;
				cnt[v][0] = cnt[k][flag];
			}
			else if(dis[v][0] == minn + w[k][j].c)
			{
				cnt[v][0] += cnt[k][flag];
			}
			else if(dis[v][1] == minn + w[k][j].c)
			{
				cnt[v][1] += cnt[k][flag];
			}
			else if(dis[v][1] > minn + w[k][j].c)
			{
				dis[v][1] = minn + w[k][j].c;
				cnt[v][1] = cnt[k][flag];
			}
		}
	}
	printf("%d %d\n", dis[e][1], cnt[e][1]);
}
int main()
{
	int s, e;
	while(~scanf("%d%d%d%d", &n, &m, &s, &e))
	{
		for(int i = 0; i < n; i++) w[i].clear();
		for(int i = 0; i < m; i++)
		{
			int u, v, c;
			scanf("%d%d%d", &u, &v, &c);
			addedge(u, v, c);
		}
		dijkstra(s, e);
	}
	return 0;
}

 

【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值