2017 JUST Programming Contest 3.0 I. Move Between Numbers
题意:n,s,e,代表有n个长度为20的数字串,问是否能从s串走到e串,可以走的条件是A,B两串sum(min(Ai,Bi)),Ai代表A中i的个数(0<=i<=9),问最短几步
思路:我们可以求出每个串可以到达其他哪个串,建图,跑从s到e的最短路,(莫名RE,开10倍过了。。。)
http://codeforces.com/gym/101502/problem/I
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int inf=1000000; 4 5 int d[300][150]; 6 struct node{ 7 int val,id; 8 node(int id,int val):id(id),val(val){} 9 bool operator <(const node &x)const{ 10 return val>x.val; 11 } 12 }; 13 bool vis[3000]; 14 int dis[3000]; 15 vector<int > g[3000]; 16 int n,s,e; 17 18 void dfs(int u){ 19 memset(vis,0,sizeof(vis)); 20 for(int i=0;i<=n+1;i++) dis[i]=inf; 21 priority_queue<node> q; 22 while(!q.empty()) q.pop(); 23 q.push(node(u,0)); 24 dis[u]=0; 25 while(!q.empty()){ 26 int cur=q.top().id; 27 q.pop(); 28 if(vis[cur]) continue; 29 vis[cur]=true; 30 for(int i=0;i<g[cur].size();i++){ 31 int v=g[cur][i]; 32 if(!vis[v]&&dis[cur]+1<dis[v]){ 33 dis[v]=dis[cur]+1; 34 q.push(node(v,dis[v])); 35 } 36 } 37 } 38 } 39 int main(){ 40 int t ; 41 cin>> t; 42 while(t--){ 43 char ch[30]; 44 scanf("%d%d%d",&n,&s,&e); 45 memset(d,0,sizeof(d)); 46 for(int i=0;i<3000;i++) g[i].clear(); 47 for(int i=1;i<=n;i++){ 48 scanf("%s",ch+1); 49 for(int j=1;j<=20;j++){ 50 d[i][ch[j]-'0']++; 51 } 52 } 53 54 for(int i=1;i<=n;i++){ 55 for(int j=i+1;j<=n;j++){ 56 int sum=0; 57 for(int k=0;k<=9;k++) 58 sum+=min(d[i][k],d[j][k]); 59 if(sum==17) { 60 g[i].push_back(j); 61 g[j].push_back(i); 62 // cout<<i<<" "<<j<<endl; 63 } 64 } 65 } 66 dfs(s); 67 if(dis[e]==inf) { 68 cout<<-1<<endl; 69 } 70 else cout<<dis[e]<<endl; 71 } 72 }