[poj2135] Farm Tour

本文介绍了一种利用最大流最小费用算法求解农场中从家到谷仓再返回的最短双路径问题的方法。通过建立超级源汇模型,将问题转化为寻找两条不重合路径的最短总距离,使用MCMF-spfa算法实现。代码示例展示了具体实现过程。
 
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17603 Accepted: 6808

Description

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000. 

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again. 

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

Input

* Line 1: Two space-separated integers: N and M. 

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length. 

Output

A single line containing the length of the shortest tour. 

Sample Input

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

Sample Output

6

Source

 
这一题建模是关键。如果我们刷两次dij什么会很麻烦,而且容易些挂。我们进而想想——我们只不过就是找两条不相重的路,我们怎么限制每条路只走一次呢?对,用流!
我们建一个超级源汇,相当于从源到汇有流量为2的流,而每条边的容量都是1(当然除了超级源与超级汇对应的边)。建模以后,我们就可以跑MCMF-spfa了(第一次写qwq)。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 const int N=1005,M=40010;
 7 int n,m,dis[N],pre[N];
 8 int tot,nxt[M],son[M],w[M],cap[M],top[M],lnk[N];
 9 bool vis[N];
10 void add(int x,int y,int co,int fl){
11     nxt[++tot]=lnk[x],son[tot]=y,w[tot]=co,cap[tot]=fl,top[tot]=x,lnk[x]=tot;
12     nxt[++tot]=lnk[y],son[tot]=x,w[tot]=-co,cap[tot]=0,top[tot]=y,lnk[y]=tot;
13 }
14 bool spfa(){
15     memset(pre,255,sizeof pre);
16     memset(dis,63,sizeof dis),dis[0]=0;
17     memset(vis,0,sizeof vis),vis[0]=1;
18     int x; queue <int> Q; while (!Q.empty()) Q.pop(); Q.push(0);
19     while (!Q.empty()){
20         x=Q.front(),vis[x]=0,Q.pop();
21         for (int j=lnk[x]; j; j=nxt[j])
22         if (cap[j]>0&&dis[son[j]]>dis[x]+w[j]){
23             dis[son[j]]=dis[x]+w[j],pre[son[j]]=j;
24             if (!vis[son[j]]) vis[son[j]]=1,Q.push(son[j]);
25         }
26     }
27     return pre[n+1]!=-1;
28 }
29 int MCMF(){
30     int ret=0;
31     while (spfa()){
32         int fl=dis[N-1];
33         for (int j=pre[n+1]; j+1; j=pre[top[j]])
34             if (fl>cap[j]) fl=cap[j];
35         for (int j=pre[n+1]; j+1; j=pre[top[j]])
36             ret+=fl*w[j],cap[j]-=fl,cap[j^1]+=fl;
37     }
38     return ret;
39 }
40 int main(){
41     scanf("%d%d",&n,&m); int x,y,z; tot=1;
42     for (int i=1; i<=m; i++)
43         scanf("%d%d%d",&x,&y,&z),add(x,y,z,1),add(y,x,z,1);
44     add(0,1,0,2),add(n,n+1,0,2);
45     printf("%d",MCMF());
46     return 0;
47 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7380554.html

下载方式:https://pan.quark.cn/s/b4d8292ba69a 在构建食品品牌的市场整合营销推广方案时,我们必须首先深入探究品牌的由来、顾客的感知以及市场环境。 此案例聚焦于一款名为“某饼干产品”的食品,该产品自1998年进入河南市场以来,经历了销售业绩的波动。 1999至2000年期间,其销售额取得了明显的上升,然而到了2001年则出现了下滑。 在先前的宣传活动中,品牌主要借助大型互动活动如ROAD SHOW来吸引顾客,但收效甚微,这揭示了宣传信息与顾客实际认同感之间的偏差。 通过市场环境剖析,我们了解到消费者对“3+2”苏打夹心饼干的印象是美味、时尚且充满活力,但同时亦存在口感腻、价位偏高、饼身坚硬等负面评价。 实际上,该产品可以塑造为兼具美味、深度与创新性的休闲食品,适宜在多种情境下分享。 这暗示着品牌需更精确地传递产品特性,同时消解消费者的顾虑。 在策略制定上,我们可考虑将新产品与原有的3+2苏打夹心进行协同推广。 这种策略的长处在于能够借助既有产品的声誉和市场占有率,同时通过新产品的加入,刷新品牌形象,吸引更多元化的消费群体。 然而,这也可能引发一些难题,例如如何合理分配新旧产品间的资源,以及如何保障新产品的独特性和吸引力不被既有产品所掩盖。 为了提升推广成效,品牌可以实施以下举措:1. **定位修正**:基于消费者反馈,重新确立产品定位,突出其美味、创新与共享的特性,减少消费者感知的缺陷。 2. **创新宣传**:宣传信息应与消费者的实际体验相契合,运用更具魅力的创意手段,例如叙事式营销,让消费者体会到产品带来的愉悦和情感共鸣。 3. **渠道选择**:在目标消费者常去的场所开展活动,例如商业中心、影院或在线平台,以提高知名度和参与度。 4. **媒体联...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值