国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

poj 2135 Farm Tour 【最小費用流】

2019-11-08 02:38:45
字體:
來源:轉載
供稿:網友

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16022 Accepted: 6208

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 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6

Source

USACO 2003 February Green

題意:求地點1到地點n再回來不經過同一條路  所走的最短路程

即1到n的F=2的最小費用;

代碼:

#include<cstdio>#include<vector>#include<algorithm>using namespace std;struct node{int to,cap,cost,rev;}OP;vector<node> P[1010];int n,m,dist[1010],pre1[1010],pre2[1010],INF=36000000;void ADD(int a,int b,int c){    OP.cap=1;OP.to=b;OP.cost=c;OP.rev=P[b].size();    P[a].push_back(OP);    OP.cap=0;OP.to=a;OP.cost=-c;OP.rev=P[a].size()-1;    P[b].push_back(OP);}int min_cost(int s,int t,int f){    int res=0;    while (f)    {        fill(dist,dist+n+1,INF);        fill(pre1,pre1+n+1,-1);        fill(pre2,pre2+n+1,-1);        dist[s]=0;        bool update=true;        while (update)        {            update=false;            for (int i=1;i<=n;i++)            {                if (dist[i]==INF) continue;                for (int j=0;j<P[i].size();j++)                {                    if (P[i][j].cap>0&&dist[i]+P[i][j].cost<dist[P[i][j].to])                    {                        update=true;                        dist[P[i][j].to]=dist[i]+P[i][j].cost;                        pre1[P[i][j].to]=i;pre2[P[i][j].to]=j;                    }                }            }        }        if (dist[t]==INF) return -1;        f--;int k=t,h;        while (pre1[k]!=-1)        {            h=pre2[k];            k=pre1[k];            P[k][h].cap--;            P[P[k][h].to][P[k][h].rev].cap++;        }        res+=dist[t];    }    return res;}int main(){    scanf("%d%d",&n,&m);    int a,b,c;    for (int i=0;i<m;i++)    {        scanf("%d%d%d",&a,&b,&c);        ADD(a,b,c);        ADD(b,a,c);    }    int ans=min_cost(1,n,2);    printf("%d/n",ans);    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 襄城县| 民乐县| 凤翔县| 陇西县| 板桥市| 且末县| 保靖县| 内黄县| 多伦县| 舞钢市| 万宁市| 温宿县| 凤冈县| 甘孜| 隆子县| 和平区| 卢龙县| 新干县| 聂拉木县| 广昌县| 察隅县| 昆山市| 永川市| 乌审旗| 沂南县| 菏泽市| 临高县| 阆中市| 读书| 尉氏县| 万荣县| 通州区| 锦屏县| 黑山县| 康保县| 乌拉特前旗| 沭阳县| 建始县| 亚东县| 南陵县| 建湖县|