码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2404 Jogging Trails

时间:2014-08-06 14:31:41      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   os   io   strong   for   

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\n",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];
}



POJ 2404 Jogging Trails,布布扣,bubuko.com

POJ 2404 Jogging Trails

标签:des   style   blog   http   os   io   strong   for   

原文地址:http://www.cnblogs.com/mfrbuaa/p/3894315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!