SPOJ TRAFFICN - Traffic Network

题目链接http://www.spoj.com/problems/TRAFFICN/

题目大意:给出一个N个顶点M条边的有向图。顶点编号1~N。给出K个双向路,问选择其中一条路能使s到t的距离最短为多少。如果无合法选择,输出-1. M < 100000 ,任意两点距离 < 1000。

解题思路:一个原图,一个反向图,两次dijkstra记录s到每个点以及t到每个点的距离。之后对于每个双向路判断一下s->u->v->t, t->u->v->s与原s->t大小即可。

代码:

 1 const int maxn = 2e5 + 5;
 2 struct Edge{
 3     int to, next, val;
 4 }; 
 5 Edge edges[maxn], edges1[maxn];
 6 int tot, head[maxn], tot1, head1[maxn];
 7 int n, m, k, s, t;
 8 bool vis[maxn];
 9 priority_queue<PII, vector<PII>, greater<PII> > q;
10 int dis[maxn], dis1[maxn];
11 
12 void init(){
13     tot = 0, tot1 = 0;
14     memset(head, -1, sizeof(head));
15     memset(head1, -1, sizeof(head1));
16 }
17 void addEdge(int u, int v, int w){
18     edges[tot].to = v;
19     edges[tot].val = w;
20     edges[tot].next = head[u];
21     head[u] = tot++;
22 }
23 void addEdge1(int u, int v, int w){
24     edges1[tot].to = v;
25     edges1[tot].val = w;
26     edges1[tot].next = head1[u];
27     head1[u] = tot++;
28 }
29 void dijk(){
30     while(!q.empty()) q.pop();
31     memset(dis, 0x3f, sizeof(dis));
32     memset(vis, 0, sizeof(vis));
33     dis[s] = 0;
34     q.push(PII(0, s));
35     while(!q.empty()){
36         int u = q.top().second; q.pop();
37         if(vis[u]) continue;
38         vis[u] = 1;
39         for(int i = head[u]; i != -1; i = edges[i].next){
40             int v = edges[i].to, w = edges[i].val;
41             if(dis[v] > dis[u] + w){
42                 dis[v] = dis[u] + w;
43                 q.push(PII(dis[v], v));
44             }
45         }
46     }
47     return;
48 }
49 void dijk1(){
50     while(!q.empty()) q.pop();
51     memset(dis1, 0x3f, sizeof(dis1));
52     memset(vis, 0, sizeof(vis));
53     dis1[t] = 0;
54     q.push(PII(0, t));
55     while(!q.empty()){
56         int u = q.top().second; q.pop();
57         if(vis[u]) continue;
58         vis[u] = 1;
59         for(int i = head1[u]; i != -1; i = edges1[i].next){
60             int v = edges1[i].to, w = edges1[i].val;
61             if(!vis[v] && dis1[v] > dis1[u] + w){
62                 dis1[v] = dis1[u] + w;
63                 q.push(PII(dis1[v], v));
64             }
65         }
66     }
67     return;
68 }
69 int main(){
70     int ca;
71     scanf("%d", &ca);
72     while(ca--){
73         init();
74         scanf("%d %d %d %d %d", &n, &m, &k, &s, &t);
75         for(int i = 0; i < m; i++){
76             int u, v, w;
77             scanf("%d %d %d", &u, &v, &w);
78             addEdge(u, v, w);
79             addEdge1(v, u, w);
80         }
81         dijk(); dijk1();
82         int ans = dis[t];
83         for(int i = 0; i < k; i++){
84             int u, v, w;
85             scanf("%d %d %d", &u, &v, &w);
86             int tp1 = dis[u] + w + dis1[v], tp2 = dis[v] + w + dis1[u];
87             if(tp1 > 0) ans = min(ans, tp1); 
88             if(tp2 > 0) ans = min(ans, tp2);
89         }
90         if(ans != inf) printf("%d\n", ans);
91         else puts("-1");
92     }
93 }

题目:

TRAFFICN - Traffic Network

 

The city traffic network consists of n nodes numbered from 1 to n and m one-way roads connecting pairs of nodes. In order to reduce the length of the shortest path between two different critical nodes s and t, a list of k two-way roads are proposed as candidates to be constructed. Your task is to write a program to choose one two-way road from the proposed list in order to minimize the resulting shortest path between s and t.

Input

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each data set, the first line contains five positive integers n (n ≤ 10 000), m (m ≤ 100 000), k (k < 300), s (1 ≤ s ≤ n), t (1 ≤ t ≤ n) separated by space. The ith line of the following m lines contains three integers di, ci, li separated by space, representing the length li ( 0< li ≤ 1000) of the ith one-way road connecting node di to ci. The jth line of the next k lines contains three positive integers uj, vj and qj (qj ≤ 1000) separated by space, representing the jth proposed two-way road of length qj connecting node uj to vj.

Output

For each data set, write on one line the smallest possible length of the shortest path after building the chosen one two-way road from the proposed list. In case, there does not exist a path from s to t, write -1.

Example

Sample Input
1
4 5 3 1 4
1 2 13
2 3 19
3 1 25
3 4 17
4 1 18
1 3 23
2 3 5
2 4 25	

Sample Output
35

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值