inf开太小错了好久……下次还是要用0x7fffffff

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int N=5024;
const int inf=0x7fffffff;
struct Edge
{
int from,to,cap,flow,cost;
};
vector<Edge>edges;
vector<int>G[N];
int n,m;
int inq[N],p[N],d[N],a[N]; void AddEdge(int from, int to,int cap, int cost)
{
Edge tp;
tp.from=from,tp.to=to,tp.cap=cap,tp.flow=0,tp.cost=cost;
edges.push_back(tp); tp.from=to,tp.to=from,tp.cap=0,tp.flow=0,tp.cost=-cost;
edges.push_back(tp); int g=edges.size();
G[from].push_back(g-2);
G[to].push_back(g-1);
} int BellmanFord(int s,int t,int &flow, int &cost)
{
int i,j,u;
for(i=0; i<=n+1; i++) d[i]=inf;
memset(inq,0,sizeof(inq));
d[s]=0;
inq[s]=1;
p[s]=0;
a[s]=inf;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
u=Q.front();
Q.pop();
inq[u]=0;
for(i=0; i<G[u].size(); i++)
{
Edge &e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(d[t]==inf) return 0;
flow+=a[t];
cost+=d[t]*a[t];
u=t;
while(u!=0)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
u=edges[p[u]].from;
}
return 1;
} int Mincost(int s,int t)
{
int flow=0,cost=0;
while(BellmanFord(s,t,flow,cost));
return cost;
} int main()
{
int i,u,v,c;
while(~scanf("%d%d",&n,&m))
{
for(i=0; i<=n+1; i++) G[i].clear();
edges.clear();
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
AddEdge(u,v,1,c);
AddEdge(v,u,1,c);
}
AddEdge(0,1,2,0);
AddEdge(n,n+1,2,0);
printf("%d\n",Mincost(0,n+1));
}
return 0;
}

最新文章

  1. SQL Server(八)——触发器
  2. 在IE7下使用angularjs(转)
  3. 【Asphyre引擎】今天终于把精灵demo基本改好了。
  4. AppScan8.0简单扫描
  5. ExtJS MVC结构
  6. Redis教程02——管道(Pipelining)
  7. oracle左右连接 完全连接 有效连接 心得总结
  8. OleDb 读取 excel
  9. Objective-C Http常用API 同步请求与异步请求
  10. hdfs的实现机制和文件系统概念
  11. Android自己定义控件(状态提示图表)
  12. 给当前的URL添加/更新新的参数
  13. 动态封杀与解封IP
  14. spring加载资源文件中classpath*与classpath的区别
  15. MySQL zip版本安装
  16. ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存
  17. centos7发行版号对应基于RHEL Source(版本)对照表
  18. centos7 搭建WEB服务器
  19. HDU 1242 Rescue(BFS+优先队列)
  20. Java动态调用类中方法

热门文章

  1. GIMP暗黑诱惑,部分彩色效果制作
  2. 关于php使用xpath解析html中文乱码问题
  3. MFC中关于子进程创建和关闭操作
  4. luogu3980 [NOI2008]志愿者招募
  5. Window Phone 8手电筒
  6. A Survey of Model Compression and Acceleration for Deep Neural Network时s
  7. Azure Storage Blob文件名区分大小写
  8. BZOJ 1933 [Shoi2007]Bookcase 书柜的尺寸 ——动态规划
  9. [luoguP3668] [USACO17OPEN]Modern Art 2 现代艺术2(栈)
  10. ElasticSearch API 之 DELETE