本文主要是介绍poj 2135 Farm Tour 最小费用流 spfa优化 16_05_14,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!