http://acm.hdu.edu.cn/showproblem.php?pid=1546

 #include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=<<; int a[maxn],n;
int g[maxn][maxn];
char str[maxn][maxn];
int dis[maxn];
bool vis[maxn]; void spfa()
{
queue<int>q;
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=inf;
dis[]=;
vis[]=true;
q.push();
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=false;
for(int i=; i<n; i++)
{
if(g[u][i]!=inf&&dis[i]>dis[u]+g[u][i])
{
dis[i]=dis[u]+g[u][i];
if(!vis[i])
{
q.push(i);
vis[i]=true;
}
}
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
for(int i=; i<n; i++)
{
cin>>a[i]>>str[i];
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(i==j) continue;
bool flag=false;
int k=strlen(str[i]);
for(int c=; c<; c++)
{
if(str[i][k+c-]!=str[j][c])
{
flag=true;
break;
}
}
if(!flag)
{
g[i][j]=a[i];
}
}
}
/*for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
printf("%d ",g[i][j]);
}
printf("\n");
}*/
spfa();
if(dis[n-]==inf) printf("-1\n");
else printf("%d\n",dis[n-]);
}
return ;
}

最新文章

  1. SQL Server 如何通过SQL语句定位SSRS中的具体报表
  2. Security4:Role 和 Permission
  3. Python 第五天 递归,计算器(2)
  4. ACM 独木舟上的旅行
  5. Scrum Meeting---Eight(2015-11-3)
  6. 细雨学习笔记:Jmeter参数化
  7. SqlServer之触发器
  8. C语言中符号格式说明
  9. Valgrind使用记录
  10. DirectFB 之 动画播放初步
  11. 使用LINQ TO XML 创建xml文档,以及读取xml文档把内容显示到GridView例子
  12. Python进阶_类与实例
  13. Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
  14. H5音乐自动播放ios//禁止安卓手机图片点击
  15. 2018SDIBT_国庆个人第四场
  16. masonry瀑布流的使用
  17. Asp.Net模拟post提交数据方法
  18. 1031 Hello World for U (20 分)
  19. php的哈希函数
  20. Showing All Messages : error: open /Users/apple/Library/Developer/Xcode/DerivedData/xxx-dkhmpttmnuppvbcxijlcxacfpzcl/Build/Products/Debug-iphoneos/xxx.app/EaseUIResource.bundle/arrow@2x.png: N

热门文章

  1. 【POJ2136】Vertical Histogram(简单模拟)
  2. 企业生产环境中linux系统分区的几种方案
  3. BNU10806:请在此处签到
  4. [RxJS] Reactive Programming - Sharing network requests with shareReplay()
  5. [MongoDB] Remove, update, create document
  6. io系统
  7. Python进阶之路---1.2python版本差异
  8. top 命令SQLServer-sybase-oracle
  9. 监控工具zabbix
  10. html中行内元素与块级元素的区别。