Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17230   Accepted: 6647

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

题意:n个点 m条路 每条路都有一个长度 现在定义从1节点出发 到n节点  再从n节点回1节点
这两条路不能有重边 问你最短路多少
其实 这可以简化成一个求做小费用的网络流,每条边的流量设为1 长度为他们的费用 
自己定义一个汇点和出发点 答案就是求流量为2的最小费用最大流 (这题目要主要 这是无向边 我也不知道为什么  反正没建就错了)
套个板子就可以了
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=0x3f3f3f3f;
const int N=+;
int head[N];
int dis[N];
int pre[N];
int vis[N];
int tot;
int m,n;
struct node{
int from,to,next,flow,cost;
}edge[N<<];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int c,int cost){
edge[tot].from=u;
edge[tot].to=v;
edge[tot].flow=c;
edge[tot].cost=cost;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].from=v;
edge[tot].to=u;
edge[tot].flow=;
edge[tot].cost=-cost;
edge[tot].next=head[v];
head[v]=tot++;
}
int spfa(int s,int t){
memset(pre,-,sizeof(pre));
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
queue<int>q;
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].flow&&dis[v]>dis[x]+edge[i].cost){
dis[v]=edge[i].cost+dis[x];
pre[v]=i;
if(vis[v]==){
vis[v]=;
q.push(v);
} }
}
}
if(pre[t]==-)return ;
return ;
}
int MCMF(int s,int t){
int flow=;
int cost=;
while(spfa(s,t)){
int minn=INF;
//cout<<3<<endl;
for(int i=pre[t];i!=-;i=pre[edge[i].from]){
minn=min(minn,edge[i].flow);
}
for(int i=pre[t];i!=-;i=pre[edge[i].from]){
edge[i].flow=edge[i].flow-minn;
edge[i^].flow=edge[i^].flow+minn;
cost=edge[i].cost+cost;
}
flow=flow+minn;
if(flow==)return cost;
}
return cost;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
add(,,,);
add(n,n+,,);
for(int i=;i<=m;i++){
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add(u,v,,cost);
add(v,u,,cost);
}
cout<<MCMF(,n+)<<endl;
}
}

最新文章

  1. Linux 添加新磁盘,在线扩充空间
  2. Java时间类型转换
  3. PHPExcel中文开发手册翻译版(1)
  4. Spring学习笔记之四----基于Annotation的Spring AOP编程
  5. Android Studio新建了一个项目看不到手机界面的效果
  6. Frenetic Python实验(一)
  7. 【BZOJ】【4011】【HNOI2015】落忆枫音
  8. 高级I/O之readn和writen函数
  9. showModalDialog-父窗体子窗体
  10. Linux学习之linux目录
  11. Dockerfile
  12. [js笔记整理]DOM 篇
  13. 查看numpy.ndarray的数据类型
  14. c#导出文件,文件名中文乱码解决方法
  15. opencv : imread()的应用
  16. 调试ucosii_pendsv中断函数有感
  17. SIFT算法大综合
  18. mysql双主+keepalived
  19. c/c++柔性数组成员
  20. 转载:gc的概念,如果A和B对象循环引用,是否可以被GC?

热门文章

  1. 利用js写全选操作
  2. Angular——todos案例
  3. ipc (进程间通信
  4. python socket 接口
  5. MIUI 的参与感
  6. JavaScript day3(转义符)
  7. 51nod1049 最大子段和【动态规划】
  8. Chat Group gym101775A(逆元,组合数)
  9. Heaters (codeforces 1066B)
  10. Mybatis操作Mysql批量更新的一个坑-&amp;allowMultiQueries=true允许批量更新