zoukankan      html  css  js  c++  java
  • POJ 2404 Jogging Trails

    Jogging Trails
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2122   Accepted: 849

    Description

    Gord is training for a marathon. Behind his house is a park with a large network of jogging trails connecting water stations. Gord wants to find the shortest jogging route that travels along every trail at least once.

    Input

    Input consists of several test cases. The first line of input for each case contains two positive integers: n <= 15, the number of water stations, and m < 1000, the number of trails. For each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 and n, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. There may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Gord's route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case.

    Output

    For each case, there should be one line of output giving the length of Gord's jogging route.

    Sample Input

    4 5
    1 2 3
    2 3 4
    3 4 5
    1 4 10
    1 3 12
    0
    

    Sample Output

    41
    

    Source

    Waterloo local 2002.07.01


    看的解题报告才懂得,看到有人说KM也能解决,实际实验了一下是不能够的,错误的原因在于我们拆点后,肯定会有对称边,我们希望最大匹配的边也是存在两两对称的,这样最后的结果除以2就能够了,可实际是匹配的边他可能不是对称的,导致了错误

    http://www.cnblogs.com/wuminye/archive/2013/05/06/3063902.html
    这人写的博客很好,能够借鉴一下
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <cstdlib>
    #include <queue>
    #define N 20
    #define M 1000000
    #define INF 0x7fffff
    using namespace std;
    int a[N][N],d[N],dis[M];
    bool inque[M];
    int n,m;
    int main()
    {
        //freopen("data.txt","r",stdin);
        int bfs(int x);
        while(scanf("%d",&n)!=EOF)
        {
            if(n==0)
            {
                break;
            }
            scanf("%d",&m);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    a[i][j] = INF;
                }
            }
            int res = 0;
            memset(d,0,sizeof(d));
            for(int i=1;i<=m;i++)
            {
                int x,y,val;
                scanf("%d %d %d",&x,&y,&val);
                d[x]++;
                d[y]++;
                res+=val;
                a[x][y] = min(a[x][y],val);
                a[y][x] = min(a[y][x],val);
            }
            for(int k=1;k<=n;k++)
            {
                for(int i=1;i<=n;i++)
                {
                    for(int j=1;j<=n;j++)
                    {
                        if(i==k||i==j||k==j)
                        {
                            continue;
                        }
                        a[i][j] = min(a[i][j],a[i][k]+a[k][j]);
                    }
                }
            }
            int sum=0,db=1;
            for(int i=1;i<=n;i++)
            {
                if(d[i]%2)
                {
                    sum+=db;
                }
                db = db*2;
            }
            int ans = bfs(sum);
            printf("%d
    ",ans+res);
        }
        return 0;
    }
    int bfs(int x)
    {
        memset(inque,false,sizeof(inque));
        for(int i=0;i<=((1<<n)-1);i++)
        {
            dis[i] = INF;
        }
        queue<int>que;
        que.push(x);
        inque[x] = true;
        dis[x] = 0;
        int op[20],op2[20];
        op2[0] = 1;
        for(int i=1;i<=n;i++)
        {
            op2[i] = op2[i-1]*2;
        }
        while(!que.empty())
        {
            x = que.front();
            que.pop();
            inque[x] = false;
            int xx = x;
            for(int i=1;i<=n;i++)
            {
                op[i] = xx%2;
                xx = xx/2;
            }
            for(int i=1;i<=n;i++)
            {
                if(op[i])
                {
                    for(int j=i+1;j<=n;j++)
                    {
                        if(op[j])
                        {
                            int y =x-op2[i-1]-op2[j-1];
                            if(dis[y]>dis[x]+a[i][j])
                            {
                                dis[y] = dis[x]+a[i][j];
                                if(!inque[y])
                                {
                                    que.push(y);
                                    inque[y] = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return dis[0];
    }
    



  • 相关阅读:
    js01
    js---18miniJquery
    js---17继承中方法属性的重写
    js---16继承
    js---16原型链
    js---15深拷贝浅拷贝 原型链
    js---14公有私有成员方法
    js---13 this call apply
    js---12对象创建方式,构造器,原型
    ESXi导出的CentOS7 ovf文件导入到workstation 无法打开GUI登录界面的问题解决方案
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3894315.html
Copyright © 2011-2022 走看看