题目链接:http://codeforces.com/problemset/problem/95/C
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths.
Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially.
At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium.
The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≤ x, y ≤ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≤ ti, ci ≤ 109), which describe the taxi driver that waits at the i-th junction — the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character.
If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
4 4
1 3
1 2 3
1 4 1
2 4 1
2 3 5
2 7
7 2
1 2
7 7
9
An optimal way — ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
题意:
有n个点,你需要从x点到y点。如果点a到点b的路径小于ta ,那么从a到b的费用就为ca。求最小费用
题解:
由于只有1000个点,所以Dij 跑n个点 ,时间复杂为 n*E*logE。你就可以得到dis[i][j](点 i 到点 j 的距离)
我们建第二幅图。如果dis[i][j] < t[i] 。 这样的话从 i 到 j 花费 ci 就可以到达。
在跑一边Dij 求最小费用。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 #include <stack> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 typedef unsigned long long uLL; 15 #define ms(a, b) memset(a, b, sizeof(a)) 16 #define pb push_back 17 #define mp make_pair 18 #define eps 0.0000000001 19 const LL INF = 0x3f3f3f3f3f3f3f3f; 20 const int inf = 0x3f3f3f3f; 21 const int mod = 1e9+7; 22 const int maxn = 1000+10; 23 struct qnode { 24 int v; 25 LL c; 26 qnode(int _v=0,int _c=0):v(_v),c(_c) {} 27 bool operator <(const qnode &r)const { 28 return c>r.c; 29 } 30 }; 31 struct Edge { 32 int v; 33 LL cost; 34 Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {} 35 }; 36 vector<Edge>E[maxn]; 37 bool vis[maxn]; 38 LL dist[maxn]; 39 void Dijkstra(int n,int start) { //点的编号从1开始 40 memset(vis,false,sizeof(vis)); 41 for(int i=1; i<=n; i++)dist[i]=INF; 42 priority_queue<qnode>que; 43 while(!que.empty())que.pop(); 44 dist[start]=0; 45 que.push(qnode(start,0)); 46 qnode tmp; 47 while(!que.empty()) { 48 tmp=que.top(); 49 que.pop(); 50 int u=tmp.v; 51 if(vis[u])continue; 52 vis[u]=true; 53 for(int i=0; i<E[u].size(); i++) { 54 int v=E[tmp.v][i].v; 55 LL cost=E[u][i].cost; 56 if(!vis[v]&&dist[v]>dist[u]+cost) { 57 dist[v]=dist[u]+cost; 58 que.push(qnode(v,dist[v])); 59 } 60 } 61 } 62 63 } 64 void addedge(int u,int v,LL w) { 65 E[u].push_back(Edge(v,w)); 66 } 67 LL t[maxn], c[maxn]; 68 LL dis[maxn][maxn]; 69 int main() { 70 #ifdef LOCAL 71 freopen("input.txt", "r", stdin); 72 // freopen("output.txt", "w", stdout); 73 #endif 74 ios::sync_with_stdio(0); 75 cin.tie(0); 76 int n, m, x, y, u, v; 77 LL w; 78 scanf("%d%d%d%d", &n, &m, &x, &y); 79 for(int i = 0; i<m; i++) { 80 scanf("%d%d%lld", &u, &v, &w); 81 addedge(u, v, w); 82 addedge(v, u, w); 83 } 84 for(int i = 1;i<=n;i++) scanf("%lld%lld", &t[i], &c[i]); 85 for(int i = 1;i<=n;i++){ 86 Dijkstra(n, i); 87 for(int j = 1;j<=n;j++) 88 dis[i][j] = dist[j]; 89 } 90 for(int i = 1;i<=n;i++) E[i].clear(); 91 for(int i = 1;i<=n;i++){ 92 for(int j = 1;j<=n;j++){ 93 if(i!=j&&dis[i][j] <= t[i]){ 94 addedge(i, j, c[i]); 95 } 96 } 97 } 98 Dijkstra(n, x); 99 if(dist[y]!=INF) 100 printf("%lld\n", dist[y]); 101 else printf("-1\n"); 102 return 0; 103 }