43. 「Loi57 test 2017.4.22」单词

统计
描述
提交
自定义测试
题目描述
给定n个串,m条关系。

每条关系包含两个单词,表示这两个单词可以相互转化。

给定起始单词和目标单词,询问起始单词最少经过多少次变化可以得到目标单词。

如果起始单词不能转化为目标单词,输出−1。

输入格式
第一行包含两个整数n,m,表示共n个单词,m条关系。

接下来n行,每行包含一个单词。

接下来m行,每行包含两个单词,表示这两个单词可以相互转化。

接下来两行,每行包含一个单词,分别表示起始单词和目标单词。数据不保证起始单词和目标单词包含在n个单词中。

输出格式
输出包含一行或两行。

如果起始单词能转化为目标单词,答案包含两行,第一行一个整数,即最少的变化次数。

第二行包含多个单词,表示起始单词变为目标单词的过程。

样例一
input

5 4
max
min
mex
tex
text
max min
min mex
mex tex
tex text
max
text
output

4
max min mex tex text
限制与约定
对于10%的数据,n≤10,m≤10

对于100%的数据,n≤10^5,m≤2×10^5每个单词的长度 ≤10.

时间限制:5s
空间限制:512MB

思路
将每一个字符串看做一个节点。题为求两节点最短路;
用map将字符串与节点标号一一映射;
记录路径(听说递归输出路径更快哦)

//spfa版
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<deque>
#include<string>
#include<map>
using namespace std;
const int maxn=100000+5;
vector<int> tu[maxn];
deque<int> Q;
int last[maxn],dis[maxn],vis[maxn];
int n,m,tot,s,e,x,y;
string c[100008],a,b;
map<string,int> num;
void build(int x,int y){
    tu[x].push_back(y);
    tu[y].push_back(x);
}
void spfa(int s){
    for(int i=1;i<=n;i++)
    dis[i]=maxn+10000;
    dis[s]=0;
    vis[s]=1;
    Q.push_back(s);
    while(!Q.empty()){
        int v=Q.front();
        Q.pop_front();
        vis[v]=0;
        for(int i=0;i<tu[v].size();i++){
            int u=tu[v][i];
            if(dis[u]>dis[v]+1){
                dis[u]=dis[v]+1;
                last[u]=v;
                if(vis[u]==0){
                    int s=Q.front();
                    if(!Q.empty()&&dis[u]<dis[s])
                    Q.push_front(u);
                    else Q.push_back(u);
                    vis[u]=1;
                }
            }
        }
    }
    return ;
}
void anse(int x){
    if(x!=s)  anse(last[x]);
    cout<<c[x]<<" ";
    return ;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        cin>>c[i];
        num[c[i]]=i;
    }
    for(int j=1;j<=m;j++){
        cin>>a>>b;
        build(num[a],num[b]);
    }
    cin>>a>>b;
    s=num[a];e=num[b];
    spfa(s);
    if(last[e]==0) {
        printf("-1");
        return 0;
    }
    printf("%d\n",dis[e]);  
    anse(e);
    return 0;
}
//bfs版
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<map>
using namespace std;
const int maxn=100000+5;
vector<int> tu[maxn];
queue<int> Q;
int last[maxn],vis[maxn];
int n,m,tot,s,e,x,y,ans;
string c[100008],a,b;
map<string,int> num;
void build(int x,int y){
    tu[x].push_back(y);
    tu[y].push_back(x);
}
void spfa(int s){
    while(!Q.empty()) Q.pop();
    Q.push(s);
    vis[s]=1;
    while(!Q.empty()){
        int v=Q.front();
        Q.pop();
        for(int j=0;j<tu[v].size();j++){
            int u=tu[v][j];
            if(vis[u]==0){
                Q.push(u);
                vis[u]=1;
                last[u]=v;
            }
            if(u==e) return ;
        }
    }
}
void anse(int x){
    if(x!=s)  {
        ans++;
    anse(last[x]);  
    }
    if(x==s) printf("%d\n",ans);
    cout<<c[x]<<" ";
    return ;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        cin>>c[i];
        num[c[i]]=i;
    }
    for(int j=1;j<=m;j++){
        cin>>a>>b;
        build(num[a],num[b]);
    }
    cin>>a>>b;
    s=num[a];e=num[b];
    spfa(s);
    if(last[e]==0) {
        printf("-1");
        return 0;
    } 
    anse(e);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我地道的翻译:The differential variational inequalities ((DVIs), for short) are useful for the study of models involving both dynamics and constraints in the form of in￾equalities. They arise in many applications: electrical circuits with ideal diodes, Coulomb friction problems for contacting bodies, economical dynamics, dynamic traffic networks. Pang and Stewart [26], [27] established the existence, unique￾ness, and Lipschitz dependence of solutions subject to boundary conditions for (DVIs) in finite dimensional spaces. Han and Pang investigated a class of dif￾ferential quasi-variational inequalities in [11], and Li, Huang and O’Regan [18] studied a class of differential mixed variational inequalities in finite dimensional Well-Posedness of Differential Mixed Quasi-Variational-Inequalities 137 spaces. Gwinner [8] obtained an equivalence result between (DVIs) and projected dynamical systems. In [9] he also proved a stability property for (DVIs) by using the monotonicity method of Browder and Minty, and Mosco set convergence. Chen and Wang [4] studied dynamic Nash equilibrium problems which have the formulation of differential mixed quasi-variational inequalities. Elastoplastic contact problems can also be incorporated into (DMQVIs) formulation because general dynamic processes in the nonsmooth unilateral contact problems are governed by quasi-variational inequalities. A numerical study for nonsmooth contact problems with Tresca friction can be found in [10], Liu, Loi and Obukhovskii [19] studied the existence and global bifurcation for periodic solutions of a class of (DVIs) by using the topological degree theory for multivalued maps and the method of guiding functions. For more details about (DVIs) we refer to [3], [30], [12], [22]–[21].
最新发布
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值