[最短路最大流] O - Marriage Match IV HDU - 3416

本文介绍了一个结合最短路径算法与最大流算法解决特定匹配问题的方法。问题背景是在两个城市之间寻找从A城到达B城的最优路径数量,其中路径不可重复使用但可以经过相同的节点多次。通过两次Dijkstra算法计算源点与汇点到各点的最短路径,并据此构造网络图,最终利用最大流算法求解最多能建立的有效连接数。

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

Marriage Match IV

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


 

Problem Description

Do not sincere non-interference。
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once. 


So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?

 

 

Input

The first line is an integer T indicating the case number.(1<=T<=65)
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.

Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.

At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.

 

 

Output

Output a line with a integer, means the chances starvae can get at most.

 

 

Sample Input

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

6 7
1 2 1
2 3 1
1 3 3
3 4 1
3 5 1
4 6 1
5 6 1
1 6

2 2
1 2 1
1 2 2
1 2

 

 

 

Sample Output

 

2

1

1

 

 

Author

starvae@HDU

 

 

Source

HDOJ Monthly Contest – 2010.06.05

 

 

#include <bits/stdc++.h>
#define P pair<int, int>
using namespace std;
 
const int inf = 0x7fffffff;
const int mn = 2000, mm = 200010;

int max_flow(int, int);
void addedge(int, int, int);

int n, m;
struct Node
{
	int from, to, val;
} E[mm];
struct Edge
{
	int to, nx, cap;
} edge1[mm], edge2[mm];
int Fr1[mn], Fr2[mn];
int dis1[mn], dis2[mn];
void dijkstra(int st, Edge edge[], int Fr[], int diss[])
{
	priority_queue<P, vector<P>, greater<P> > que;
	for (int i = 0; i <= n; i++)
		diss[i] = inf;
		
	diss[st] = 0;
	que.push(P(0, st));
	
	while (!que.empty())
	{
		P p = que.top();
		que.pop();
		if (diss[p.second] < p.first)
			continue;
		int v = p.second;
		for (int i = Fr[v]; i != -1; i = edge[i].nx)
		{
			if (diss[edge[i].to] > diss[v] + edge[i].cap)
			{
				diss[edge[i].to] = diss[v] + edge[i].cap;
				que.push(P(diss[edge[i].to], edge[i].to));
			}
		}
	}
}


int edge;
int fr[mn];
int cur[mn];  // 当前弧优化
int lv[mn];
void init()
{
	edge = 0;
	memset(fr, -1, sizeof fr);
	memset(Fr1, -1, sizeof Fr1);
	memset(Fr2, -1, sizeof Fr2);
}
 
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("D:\\in.txt", "r", stdin);
	#endif // ONLINE_JUDGE
	
	int T;
	scanf("%d", &T);
	while (T--)
	{
		init();
		scanf("%d %d", &n, &m);
		for (int i = 1; i <= m; i++)
		{
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			E[i].from = a, E[i].to = b, E[i].val = c;
			edge1[i].to = b, edge1[i].nx = Fr1[a], edge1[i].cap = c;
			Fr1[a] = i;
			edge2[i].to = a, edge2[i].nx = Fr2[b], edge2[i].cap = c;
			Fr2[b] = i;
		}
		
		int A, B;
		scanf("%d %d", &A, &B);
		
		/// 两次dijkstra求出源点和汇点到每个点的最短路
		dijkstra(A, edge1, Fr1, dis1);
		dijkstra(B, edge2, Fr2, dis2);
		int DIS = dis1[B];
		
		for (int i = 1; i <= m; i++)
		{
			/// 如果源点到当前边的起点的距离+边长+终点到汇点的距离 = 源点到汇点的最短距离
			/// 则当前边为从源点到汇点最短路中的一条边
			if (dis1[E[i].from] + E[i].val + dis2[E[i].to] == DIS)
				addedge(E[i].from, E[i].to, 1);
		}
		
		printf("%d\n", max_flow(A, B)); /// 求最大流
	}

	return 0;
}


struct node
{
	int to, val, nx, fan;
} e[mm];
 
void addedge(int u, int v, int w)
{
	edge++;
	e[edge].to = v, e[edge].val = w, e[edge].nx = fr[u], e[edge].fan = edge + 1;
	fr[u] = edge;
	edge++;
	e[edge].to = u, e[edge].val = 0, e[edge].nx = fr[v], e[edge].fan = edge - 1;
	fr[v] = edge;
}
 
void bfs(int s)
{
	memset(lv, 0, sizeof lv);
	lv[s] = 1;
	queue<int> q;
	q.push(s);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = fr[t]; i != -1; i = e[i].nx)
		{
			if (e[i].val > 0 && !lv[e[i].to])
			{
				lv[e[i].to] = lv[t] + 1;
				q.push(e[i].to);
			}
		}
	}
}
 
int dfs(int s, int t, int f)
{
	if (s == t)
		return f;
	for (int &i = cur[s]; i != -1; i = e[i].nx)
	{
		if (e[i].val > 0 && lv[s] < lv[e[i].to])
		{
			int d = dfs(e[i].to, t, min(f, e[i].val));
			if (d > 0)
			{
				e[i].val -= d;
				e[e[i].fan].val += d;
				return d;
			}
		}
	}
	return 0;
}
 
int max_flow(int s, int t)
{
	int flow = 0;
	while (1)
	{
		bfs(s);
		if (!lv[t])
			break;

		for (int i = 0; i <= n; i++)
			cur[i] = fr[i];
		int f = 0;
		while ((f = dfs(s, t, inf)) > 0)
			flow += f;
	}
	return flow;
}

 

关于“Marriage Matching 3190”,经过网络搜索发现这可能是一个特定编号的问题或者算法题目,通常出现在编程竞赛、学术研究或计算机科学领域中的匹配问题讨论中。以下是对此主题的详细解答: --- ### 方法一:理解婚姻匹配的基本概念 婚姻匹配问题是图论和组合优化领域的经典问题之一,主要涉及如何在一组男性和女性之间找到稳定的配对关系。稳定婚配问题(Stable Marriage Problem, SMP)由Gale和Shapley于1962年提出,其核心目标是在给定偏好列表的情况下避免不稳定配对。 对于编号为3190的具体问题,可能是某个平台上的变种版本,例如增加约束条件或调整输入输出形式。 --- ### 方法二:实现 Gale-Shapley 算法解决基本问题 经典的解决方案是使用 Gale-Shapley 算法来求解稳定婚配问题。该算法的核心思想如下: 1. 每位单身男子向他尚未求婚过的喜欢女子求婚。 2. 女子从当前收到的所有求婚者中选择她喜欢的未婚男子,并暂时接受他的求婚。 3. 如果某女子已经订婚但收到了更优的选择,则解除现有订婚并将原伴侣重新归入单身状态。 4. 重复以上过程直到所有人都成功配对为止。 下面是 Gale-Shapley 算法的一个简单 Python 实现: ```python def gale_shapley(men_prefs, women_prefs): n = len(men_prefs) free_men = list(range(n)) engaged = [-1] * n proposals = [0] * n while free_men: man = free_men.pop(0) woman = men_prefs[man][proposals[man]] proposals[man] += 1 if engaged[woman] == -1: engaged[woman] = man else: current_man = engaged[woman] if women_prefs[woman].index(man) < women_prefs[woman].index(current_man): free_men.append(current_man) engaged[woman] = man else: free_men.append(man) return {w: m for w, m in enumerate(engaged)} # 示例数据 men_prefs = [[0, 1, 2], [1, 2, 0], [2, 0, 1]] women_prefs = [[1, 2, 0], [0, 1, 2], [2, 1, 0]] result = gale_shapley(men_prefs, women_prefs) print(result) ``` --- ### 方法三:针对特殊编号问题的扩展思考 如果“3190”代表某种特殊的限制条件或复杂场景,可以考虑以下方向进行改进: - **加权匹配**:引入权重表示每对之间的亲密度或其他指标,寻找总权重大的匹配方案。 - **多对多匹配**:允许一个人同时与多人建立联系,适用于社交网络分析等实际应用。 - **动态更新机制**:当参与者的偏好随时间变化时,设计实时调整策略以保持稳定性。 具体实现需要结合问题描述进一步明确规则及边界情况。 --- ### 方法四:查找在线资源获取完整信息 由于“Marriage Matching 3190”可能来源于某些特定网站或书籍章节,建议访问以下渠道了解详情: - 编程竞赛平台(如LeetCode、Codeforces、AtCoder),搜索关键词“marriage matching”查看是否有对应编号的任务; - 学术论文数据库(Google Scholar、ResearchGate),查阅有关SMP及其衍生模型的研究成果; - 开源社区论坛(Stack Overflow、GitHub Issues),与其他开发者交流经验心得。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值