HDU 1546 Idiomatic Phrases Game

成语接龙。

上一个的尾必须和下一个的首相同。注意:花费的时间是上一个。

一开始我就建图建错了。

比如第 i 个成语 与第 j 个成语, 第  i 个成语前面的时间为 t ;

建图为 i -> j = t;

基友说这《图论算法理论、实现及应用》上有一样的题,我借来看了一下,发现它建图似乎有错误。

0->3 这条边的权值似乎错了。

反正我的建图是这样的

0->1=5;0->3=5;

1->2=5;

2->4=7;

3->4=15;

求 0 -> 4 的最短路。

只要建图不错误,就是求一个最短路问题而已。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<iostream>
#define INF 0x7fffffff
using namespace std;
int n,m;
bool vis[1101];
int d[1101];
struct lx
{
    int v,t;
};
vector<lx>g[1101];
map<string,int>word;
string str[1001];
int strt[1001];
void SPFA(int start,int thend)
{
    for(int i=0;i<1101;i++)
        d[i]=INF,vis[i]=0;
    queue<int>q;
    vis[start]=1,d[start]=0;
    q.push(start);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int j=0;j<g[u].size();j++)
        {
            int v=g[u][j].v;
            int t=g[u][j].t;
            if(d[v]>d[u]+t)
            {
                d[v]=d[u]+t;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(d[thend]!=INF)
    printf("%d\n",d[thend]);
    else puts("-1");
}
void build()
{
    int cot=0;
    vector<int>head[1001];
    for(int i=1;i<=m;i++)
    {
        string a=str[i].substr(0,4);
        if(word[a]==0)
        {
            word[a]=cot++;
            head[word[a]].push_back(i);
        }
        else
            head[word[a]].push_back(i);
    }
    for(int i=1;i<=m;i++)
    {
        int len=str[i].length();
        string a=str[i].substr(len-4,4);
        if(word[a]!=0)
        {
            lx now;int k=word[a];
            for(int j=0;j<head[k].size();j++)
            {
                now.t=strt[i];
                now.v=head[k][j];
                g[i].push_back(now);
            }
        }
    }
}
int main()
{
    while(scanf("%d",&m),m)
    {
        for(int i=0;i<1101;i++)
            g[i].clear();
        word.clear();
        for(int i=1;i<=m;i++)
            cin>>strt[i]>>str[i];
        build();
        /*for(int i=1;i<=m;i++)
        {
            for(int j=0;j<g[i].size();j++)
                printf("%d->%d:%d ",i,g[i][j].v,g[i][j].t);
            printf("\n");
        }*/
        SPFA(1,m);
    }
}

HDU 1546 Idiomatic Phrases Game,布布扣,bubuko.com

时间: 2024-10-10 05:37:46

HDU 1546 Idiomatic Phrases Game的相关文章

hdu 1546 Idiomatic Phrases Game 最短路

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2283    Accepted Submission(s): 742 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

HDU 1546 Idiomatic Phrases Game(dijkstra+优先队列)

Idiomatic Phrases Game Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the li

Idiomatic Phrases Game HDU 1546

Description Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with the two

Idiomatic Phrases Game(图论最短路)

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3504    Accepted Submission(s): 1182 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

ZOJ 2750 Idiomatic Phrases Game

Idiomatic Phrases Game Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends w

hdu1546——Idiomatic Phrases Game

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2191    Accepted Submission(s): 712 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

hdu 1546

Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with

hdu 1546(dijkstra)

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3154    Accepted Submission(s): 1026 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

Idiomatic Phrases Game 成语接龙SPFA+map

Idiomatic Phrases Game Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends w