poj 2135 Farm Tour 最小费用流 spfa优化 16_05_14
来源:互联网 发布:win7服务器端口开放 编辑:程序博客网 时间:2024/06/08 10:09
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
const int N = 500;
const int M=20000;
struct edge{
int to,cap,cost,rev;
};
vector<edge> G[1005];
int dist[1005],inq[1005],prev[1005],prel[1005];
int n,m,x,y,c;
void add_edge(int u,int v,int cost)
{
G[u].push_back(edge{v,1,cost,G[v].size()});
G[v].push_back(edge{u,0,-cost,G[u].size()-1});
//cout<<u<<" "<<G[u].size()<<endl;
}
int mincost(int s,int t,int f)
{
int ans=0;
while(f>0)
{
memset(dist,inf,sizeof(dist));
memset(inq,0,sizeof(inq));
dist[s]=0;
queue<int> q;
q.push(s);
inq[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();inq[u]=0;
for(int j=0;j<G[u].size();j++)
{
edge &e=G[u][j];
if(e.cap>0&&dist[e.to]>dist[u]+e.cost)
{
dist[e.to]=dist[u]+e.cost;
prev[e.to]=u;
prel[e.to]=j;
if(!inq[e.to])
{
q.push(e.to);
inq[e.to]=1;
}
}
}
}
for(int i=t;i>s;)
{
int f=prev[i];
int j=prel[i];
G[f][j].cap-=1;
G[i][G[f][j].rev].cap+=1;
ans+=G[f][j].cost;
i=prev[i];
}
f-=1;
}
return ans;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&c);
add_edge(x,y,c);
add_edge(y,x,c);
}
printf("%d\n",mincost(1,n,2));
}
return 0;
}
上面是SPFA,下面是朴素的bellman
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
const int N = 500;
const int M=20000;
struct edge{
int to,cap,cost,rev;
};
vector<edge> G[1005];
int dist[1005],inq[1005],prev[1005],prel[1005];
int n,m,x,y,c;
void add_edge(int u,int v,int cost)
{
G[u].push_back(edge{v,1,cost,G[v].size()});
G[v].push_back(edge{u,0,-cost,G[u].size()-1});
//cout<<u<<" "<<G[u].size()<<endl;
}
int mincost(int s,int t,int f)
{
int ans=0;
while(f>0)
{
memset(dist,inf,sizeof(dist));
dist[s]=0;
bool update=true;
while(update)
{
update=false;
for(int u=1;u<n;u++)
for(int j=0;j<G[u].size();j++)
{
edge &e=G[u][j];
if(e.cap>0&&dist[e.to]>dist[u]+e.cost)
{
dist[e.to]=dist[u]+e.cost;
prev[e.to]=u;
prel[e.to]=j;
update=true;
//cout<<"4"<<endl;
}
}
}
for(int i=t;i>s;)
{
int f=prev[i];
int j=prel[i];
G[f][j].cap-=1;
G[i][G[f][j].rev].cap+=1;
ans+=G[f][j].cost;
i=prev[i];
}
f-=1;
}
return ans;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&c);
add_edge(x,y,c);
add_edge(y,x,c);
}
printf("%d\n",mincost(1,n,2));
}
return 0;
}
分析:两条路不能有任意一条公共边,就决定了这道题目只能用流量为2的最小费用流,而不是最短路
下面是wa的代码:注意边的连接:无向路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
const int N = 500;
const int M=20000;
struct edge{
int to,cap,cost,rev;
};
vector<edge> G[1005];
int dist[1005],inq[1005],prev[1005],prel[1005];
int n,m,x,y,c;
void add_edge(int u,int v,int cost)
{
G[u].push_back(edge{v,1,cost,G[v].size()});
G[v].push_back(edge{u,0,-cost,G[u].size()-1});
//cout<<u<<" "<<G[u].size()<<endl;
}
int mincost(int s,int t,int f)
{
int ans=0;
while(f>0)
{
memset(dist,inf,sizeof(dist));
dist[s]=0;
bool update=true;
while(update)
{
update=false;
for(int u=1;u<n;u++)
for(int j=0;j<G[u].size();j++)
{
edge &e=G[u][j];
if(e.cap>0&&dist[e.to]>dist[u]+e.cost)
{
dist[e.to]=dist[u]+e.cost;
prev[e.to]=u;
prel[e.to]=j;
update=true;
//cout<<"4"<<endl;
}
}
}
for(int i=t;i>s;)
{
int f=prev[i];
int j=prel[i];
G[f][j].cap-=1;
G[i][G[f][j].rev].cap+=1;
ans+=G[f][j].cost;
i=prev[i];
}
f-=1;
}
return ans;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&c);
add_edge(x,y,c);
add_edge(y,x,c);
}
printf("%d\n",mincost(1,n,2));
}
return 0;
}
- poj 2135 Farm Tour 最小费用流 spfa优化 16_05_14
- POJ 2135 Farm Tour(最小费用流)
- POJ 2135 Farm Tour(最小费用流)
- POJ 2135 Farm Tour 最小费用流
- POJ 2135 Farm Tour【最小费用流】
- poj 2135 Farm Tour(最小费用流)
- poj 2135 Farm Tour 最小费用流
- poj 2135 Farm Tour 【最小费用流】
- POJ 2135 Farm Tour 最小费用流
- Farm Tour-最小费用最大流/SPFA
- poj 2135 Farm Tour (最小费用流成圈)
- (最小费用) POJ 2135 Farm Tour
- poj 2135 Farm Tour(最小费用问题)
- POJ 2135 Farm Tour (最小费用最大流)
- POJ 2135 Farm Tour 最小费用最大流
- POJ 2135 Farm Tour (最小费用最大流)
- poj 2135 Farm Tour (最小费用最大流模板)
- POJ 2135 Farm Tour(最小费用最大流)
- CF#318 (Div. 2)B. Bear and Three Musketeers 暴力 复杂度分析
- hdu 1421
- poj 3041 Asteroids 二分匹配 匈牙利算法 模板题
- poj 3281 Dining 最大流dinic 模板题
- poj 3469 Dual Core CPU 最大流建图思想 dinic 弧优化很重要
- poj 2135 Farm Tour 最小费用流 spfa优化 16_05_14
- poj 3686 The Windy\'s 最小费用流 建图 16_05_14
- poj 3168 Barn Expansion 平面扫描+线段相交问题
- K最短路问题(A*算法)
- Kth Smallest Element in a Sorted Matrix (第十二周 二分法)
- powerdesign preview 中注释语句在column外面解决方法
- poj 2482 挑战 284
- poj 2686 Traveling by Stagecoach 状态压缩
- hdu 3790 最短路模板