UVa10150Problem B: Doublets

#include <cstring>
#include <string>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>

using namespace std;

const int N = 20;
const int M = 25500;
const int INF = 0x3f3f3f3f;

struct Edge
{
    int from, to, w;
};

struct HeapNode
{
    int d, u;
    bool operator < (const HeapNode &other) const
    {
        return d > other.d;
    }
};

map<string, int> strMap;
map<int, string> intMap;
int cnt;
vector<string> dict;
vector<Edge> vecEdge;
vector<int> adjList[M];
int d[M], p[M];
bool vis[M];

bool ok(const string &s1, const string &s2);
void addEdge(int u, int v, int w);
void dijkstra(int s, int t);

int main()
{
    static char buf[N], buf2[N];
    string word;
    string source, dest;
    int kase = 1;
    cnt = 0;

#ifndef ONLINE_JUDGE
    freopen("e:\\uva_in.txt", "r", stdin);
#endif

    while (gets(buf)) {
        word = buf;
        if (word == "") break;
        if (!strMap.count(word)) {
            strMap[word] = cnt;
            intMap[cnt] = word;
            cnt++;

            for (size_t i = 0; i < dict.size(); i++) {
                if (ok(dict[i], word)) {
                    int u = strMap[dict[i]];
                    int v = strMap[word];
                    addEdge(u, v, 1);
                }
            }
            dict.push_back(word);
        }
    }

    while (scanf("%s%s", buf, buf2) == 2) {

        if (kase != 1) printf("\n");
        kase++;

        source = buf, dest = buf2;
        if (source.length() != dest.length()) {
            printf("No solution.\n");
            continue;
        }

        int s = strMap[source];
        int t = strMap[dest];
        dijkstra(s, t);
    }

    return 0;
}

bool ok(const string &s1, const string &s2)
{
    if (s1.size() != s2.size()) return false;

    int cnt = 0;
    for (size_t i = 0; i < s1.length(); i++) {
        if (s1[i] != s2[i]) cnt++;
        if (cnt > 1) return false;
    }

    return cnt == 1;
}

void addEdge(int u, int v, int w)
{
    vecEdge.push_back((Edge){u, v, w});
    vecEdge.push_back((Edge){v, u, w});

    int size = vecEdge.size();

    adjList[u].push_back(size - 2);
    adjList[v].push_back(size - 1);
}

void dijkstra(int s, int t)
{
    priority_queue<HeapNode> pq;

    for (int i = 0; i < cnt; i++) {
        d[i] = (i == s ? 0 : INF);
        p[i] = -1;
        vis[i] = false;
    }

    pq.push((HeapNode){d[s], s});

    while (!pq.empty()) {
        HeapNode node = pq.top(); pq.pop();

        int u = node.u;
        if (u == t) break;

        if (vis[u]) continue;
        vis[u] = true;

        for (size_t i = 0; i < adjList[u].size(); i++) {
            int &e = adjList[u][i];
            int v = vecEdge[e].to;
            int w = vecEdge[e].w;
            if (d[u] + w < d[v]) {
                d[v] = w + d[u];
                p[v] = u;
                pq.push((HeapNode){d[v], v});
            }
        }
    }

    if (d[t] == INF) {
        printf("No solution.\n");
    } else {
        static int ans[M];
        int top = 0;

        while (t != -1) {
            ans[top++] = t;
            t = p[t];
        }

        for (int i = top - 1; i >= 0; i--) {
            printf("%s\n", intMap[ans[i]].c_str());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值