DIJK,最短路,建两个图就好了。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int n, m, D1, D2, A, B;
vector<string>vs[];
int mp1[N][N];
int mp2[N][N]; void conn(int mp[][N], const string & s, int val) {
int t = ;
vector<int>vt;
for (int i = ; i < s.length(); ++i) {
if (s[i] != '+') t = t * + s[i] - '';
else {
vt.push_back(t);
t = ;
}
}
vt.push_back(t);
for (int i = ; i < (int)vt.size(); ++i) {
for (int j = i+; j < (int)vt.size(); ++j) {
mp[vt[i]][vt[j]] = min(mp[vt[i]][vt[j]], val*(j-i));
}
}
} int dis[N];
char vis[N]; int dijkstra(int mp[][N]) {
memset(dis, 0x3f, sizeof (dis));
memset(vis, , sizeof (vis));
dis[A] = ;
for (int i = ; i < n; ++i) {
int Min = inf, p;
for (int j = ; j <= n; ++j) {
if (!vis[j] && Min > dis[j]) {
Min = dis[j];
p = j;
}
}
if (p == B) break;
vis[p] = ;
for (int j = ; j <= n; ++j) {
if (!vis[j] && dis[p]+mp[p][j] < dis[j]) {
dis[j] = dis[p] + mp[p][j];
}
}
}
return dis[B];
} void solve() {
memset(mp1, 0x3f, sizeof (mp1));
memset(mp2, 0x3f, sizeof (mp2));
for (int i = ; i < (int)vs[].size(); ++i) {
conn(mp1, vs[][i], D1);
}
for (int i = ; i < (int)vs[].size(); ++i) {
conn(mp1, vs[][i], D1);
conn(mp2, vs[][i], D2);
}
int ans = min(dijkstra(mp1), dijkstra(mp2));
printf(ans == inf ? "-1\n" : "%d\n", ans);
} int main() {
int T, k;
char str[];
scanf("%d", &T);
while (T--) {
vs[].clear();
vs[].clear();
scanf("%d %d", &n, &m);
for (int i = ; i < m; ++i) {
scanf("%s %d", str, &k);
if (k == ) vs[].push_back(str);
else vs[].push_back(str);
}
scanf("%d %d %d %d", &D1, &D2, &A, &B);
solve();
}
return ;
}

HDU 4522的更多相关文章

  1. HDU 4522 (恶心建图)

    湫湫系列故事——过年回家 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  2. hdu 4522(图论,构图)

    湫湫系列故事——过年回家 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. 最少拦截系统------LCS--------动态规划

    这是一道极好的题,会了这个应该说 最长递增子序列什么的  就有了另外一种思路了 下面附上代码---应该仔细的看一下  那个  if判断 #include<stdio.h> #include ...

  2. sessionStorage 的使用

    sessionStorage 的使用: sessionStorage.removeItem("data"); sessionStorage.getItem("data&q ...

  3. CF 436D 最小生成树

    设一个开头的虚节点,然后建稠密图,O(n^2).使用prim.O(n^2),保存方案,没什么好说的. #include <string.h> #include <stdio.h> ...

  4. python网络爬虫数据中的三种数据解析方式

    一.正则解析 常用正则表达式回顾: 单字符: . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [0-9] \D : 非数字 \w :数字.字母.下划线 ...

  5. 用python语言写一个简单的计算器

    假如我们有这样一个式子: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2 ...

  6. ACM_汉诺塔问题(递推dp)

    Problem Description: 最近小G迷上了汉诺塔,他发现n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于发生错移产生的系列就增加了,这种错误是放错了 ...

  7. 在Swift中,如何像Objective-C定义可选接口?

    Objective-C中的protocol里存在@optional关键字,被这个关键字修饰的方法并非必须要被实现.我们可以通过接口定义一系列方法,然后由实现接口的类选择性地实现其中几个方法.在Coco ...

  8. Android sensor 系统框架 (一)

    这几天深入学习了Android sensor框架,以此博客记录和分享分析过程,其中难免会有错误的地方,欢迎指出! 这里主要分析KERNEL->HAL->JNI这3层的流程.主要从以下几方面 ...

  9. C语言保留字

    数值变量相关: int float double char long short unsigned signed 储存说明符 const 用于声明常量 static用于限制变量/函数的作用范围等等 e ...

  10. PHP 之CURL请求封装GET、POST、PUT、DELETE

    /** * @Description: curl请求 * @Author: Yang * @param $url * @param null $data * @param string $method ...