费用流!

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

基本思路:
    把弧<i,j>的单位费用w[i,j]看作弧<i,j>的路径长度,每次找从源点s到汇点t长度最短(费用最小)的可增广路径进行增广。
1. 最小费用可增广路
2. 路径s到t的长度即单位流量的费用。
其实就是把ek算法中的广搜改成spfa……(因为存在负权边所以得用bellman-ford或spfa)
<pre name="code" class="cpp">#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define M 1100
const int inf=0x7fffffff;
struct node
{
    int u,v,c,f,next;  //C为花费,F为flow流量
}e[M*40];
int pre[M],dis[M],head[M],t;
int vis[M];
void add1(int u,int v,int c,int f)
{
    e[t].u=u;
    e[t].v=v;
    e[t].c=c;
    e[t].f=f;
    e[t].next=head[u];
    head[u]=t++;
}
void add(int u,int v,int c,int f)
{
    add1(u,v,c,f);      
    add1(v,u,-c,0);  //反向边流量初始为零,如果走反向边费用正好和原边抵消
}
int spfa(int s,int t)
{
    int i,u,v;
    queue<int>q;
    q.push(s);
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    for(i=s;i<=t;i++)
        dis[i]=inf;
    dis[s]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(i=head[u];i!=-1;i=e[i].next)
        {
            v=e[i].v;
            if(e[i].f&&dis[v]>dis[u]+e[i].c)  //找到一条最小费用流
            {
                dis[v]=dis[u]+e[i].c;  
                pre[v]=i;       //记录路径
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
        vis[u]=0;
    }
    if(dis[t]!=inf)
        return 1;
    return 0;
}
void solve(int s,int t)
{
    int ans=0,i,j;
	int flow=0,cost=0;     //总流量、总费用
    while(spfa(s,t))
    {
		int minf=inf;
		for(i=pre[t];i!=-1;i=pre[e[i].u])
		{
			if(e[i].f<minf)
				minf=e[i].f;
		}
		flow+=minf;   //该条路径的流量
        for(i=pre[t];i!=-1;i=pre[e[i].u])
        {
            j=i^1;
            e[i].f-=minf;
            e[j].f+=minf;
        }
	cost+=dis[t]*minf;   //单位运费和乘以流量得费用   
    }
    printf("%d\n",cost);
}
int main()
{
    int i,u,v,c,n,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        t=0;
        memset(head,-1,sizeof(head));
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&c);  //边长度看做单位运费
            add(u,v,c,1);
            add(v,u,c,1);       //无向边,费用为长度,流量为1(只能通过一次)
        }
//往返的两条线路可以看成是从源点到汇点的两条线路,
//所以只要从附加源点向源点连一条容量为2的边和从汇点向附加汇点连一条容量为2的边就可以限线路为两条.
        add(0,1,0,2);
        add(n,n+1,0,2);
        solve(0,n+1);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园的建设目标是通过数据整合、全面共享,实现校园内教学、科研、管理、服务程的数字化、信息化、智能化和多媒体化,以提高资源利用率和管理效率,确保校园安全。 智慧校园的建设思路包括构建统一支撑平台、建立完善管理体系、大数据辅助决策和建设校园智慧环境。通过云架构的数据中心与智慧的学习、办公环境,实现日常教学活动、资源建设情况、学业水平情况的全面统计和分析,为决策提供辅助。此外,智慧校园还涵盖了多媒体教学、智慧录播、电子图书馆、VR教室等多种教学模式,以及校园网络、智慧班牌、校园广播等教务管理功能,旨在提升教学品质和管理水平。 智慧校园的详细方案设计进一步细化了教学、教务、安防和运维等多个方面的应用。例如,在智慧教学领域,通过多媒体教学、智慧录播、电子图书馆等技术,实现教学资源的共享和教学模式的创新。在智慧教务方面,校园网络、考场监控、智慧班牌等系统为校园管理提供了便捷和高效。智慧安防系统包括视频监控、一键报警、阳光厨房等,确保校园安全。智慧运维则通过综合管理平台、设备管理、能效管理和资产管理,实现校园设施的智能化管理。 智慧校园的优势和价值体现在个性化互动的智慧教学、协同高效的校园管理、无处不在的校园学习、全面感知的校园环境和轻松便捷的校园生活等方面。通过智慧校园的建设,可以促进教育资源的均衡化,提高教育质量和管理效率,同时保障校园安全和提升师生的学习体验。 总之,智慧校园解决方案通过整合现代信息技术,如云计算、大数据、物联网和人工智能,为教育行业带来了革命性的变革。它不仅提高了教育的质量和效率,还为师生创造了一个更加安全、便捷和富有智慧的学习与生活环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值