当前位置: 首页 > 工具软件 > Postal mail > 使用案例 >

UVA117 POJ1237 The Postal Worker Rings Once【Floyd算法+欧拉路径】

濮阳征
2023-12-01

The Postal Worker Rings Once
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1011 Accepted: 560

Description

Graph algorithms form a very important part of computer science and have a lineage that goes back at least to Euler and the famous Seven Bridges of Konigsberg problem. Many optimization problems involve determining efficient methods for reasoning about graphs.

This problem involves determining a route for a postal worker so that all mail is delivered while the postal worker walks a minimal distance, so as to rest weary legs.
Given a sequence of streets (connecting given intersections) you are to write a program that determines the minimal cost tour that traverses every street at least once. The tour must begin and end at the same intersection.

The ``real-life’’ analogy concerns a postal worker who parks a truck at an intersection and then walks all streets on the postal delivery route (delivering mail) and returns to the truck to continue with the next route.

The cost of traversing a street is a function of the length of the street (there is a cost associated with delivering mail to houses and with walking even if no delivery occurs).

In this problem the number of streets that meet at a given intersection is called the degree of the intersection. There will be at most two intersections with odd degree. All other intersections will have even degree, i.e., an even number of streets meeting at that intersection.

Input

The input consists of a sequence of one or more postal routes. A route is composed of a sequence of street names (strings), one per line, and is terminated by the string ``deadend’’ which is NOT part of the route. The first and last letters of each street name specify the two intersections for that street, the length of the street name indicates the cost of traversing the street. All street names will consist of lowercase alphabetic characters.

For example, the name foo indicates a street with intersections f and o of length 3, and the name computer indicates a street with intersections c and r of length 8. No street name will have the same first and last letter and there will be at most one street directly connecting any two intersections. As specified, the number of intersections with odd degree in a postal route will be at most two. In each postal route there will be a path between all intersections, i.e., the intersections are connected.

Output

For each postal route the output should consist of the cost of the minimal tour that visits all streets at least once. The minimal tour costs should be output in the order corresponding to the input postal routes.

Sample Input

one
two
three
deadend
mit
dartmouth
linkoping
tasmania
york
emory
cornell
duke
kaunas
hildesheim
concord
arkansas
williams
glasgow
deadend

Sample Output

11
114

Source

Duke Internet Programming Contest 1992,UVA 117

问题链接UVA117 POJ1237 The Postal Worker Rings Once
问题简述:(略)
问题分析:最短路与欧拉路径问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA117 POJ1237 The Postal Worker Rings Once */

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>

using namespace std;

/* Floyd-Warshall算法:计算图中任意2点之间的最短距离
 * 复杂度:O(N×N×N)
 * 输入:n 全局变量,图结点数
 *      g 全局变量,邻接矩阵,g[i][j]表示结点i到j间的边距离
 * 输出:g 全局变量
 */
const int INF = 0x3f3f3f3f;
const int N = 26;
int g[N][N];

void floyd()
{
    for(int k = 0; k < N; k++)
        for(int i = 0; i < N; i++)
            for(int j = 0; j < N; j++)
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}

int degree[N];

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string s;
    int ans = 0;
    memset(g, INF, sizeof(g));
    memset(degree, 0, sizeof(degree));
    while(cin >> s) {
        if(s == "deadend") {
            int u = -1, v = -1;
            for(int i = 0; i < N; i++)
                if(degree[i] & 1) {
                    if(u == -1) u = i;
                    else v = i;
                }

            floyd();

            if(u != -1) printf("%d\n", ans + g[u][v]);
            else printf("%d\n", ans);

            memset(g, INF, sizeof(g));
            memset(degree, 0, sizeof(degree));
            ans = 0;
        } else {
            g[s[s.size() - 1] - 'a'][s[0] - 'a'] = g[s[0] - 'a'][s[s.size() - 1] - 'a'] = s.size();
            degree[s[s.size() - 1] - 'a']++;
            degree[s[0] - 'a']++;
            ans += s.size();
        }
    }

    return 0;
}
 类似资料: