admin 管理员组

文章数量: 1087677

【upc】山路 (ghat)

问题 H: 山路 (ghat)

时间限制: 1 Sec  内存限制: 128 MB
提交 状态

题目描述

会和神奈子一起改变地形,开凿地下洞穴等。虽说是一起,不过看起来改变土地是诹访子的工作。与其说她是直接将大地整平,不如说这是她麾下的崇神的功劳。——「求闻口授」

山路交错相同,令人烦躁。
于是诹访子想要将山路重新规划,具体的说,山路可以看成 n 个点,m  条边的无向图。她会在这幅图上的基础之上添加一些边,具体的说,她会给每个点设一个权值ai ,然后将点两两之间连边,假如连了一条边为(i,j) ,那么这条边的长度为|ai + k × aj|  ,其中 k=1 或 -1。
几千年过去之后,已经没什么人再记得曾经有诹访子这样一位神,然而山路却完整地保留到了今天。
那么你作为一个普通人,现在想要从 1 号点走到 n 号点去,你想知道最短的路径长度是多少?

输入

第一行三个数:n,m,k 。
接下来 m 行:每行三个数ui,vi,wi  ,描述一条边连接 ui,vi 长度为 wi。
接下来一行: n个整数,第 i 个整数表示ai。

输出

一行一个整数代表答案。

样例输入 Copy

3 1 -1
1 2 3
1 7 8

样例输出 Copy

4

题目大意:

中文题意

题目思路:

嗯...

鸽了一周,最后在铭宇巨巨的催促下,昨晚ko

其实想麻烦了,图论题无非就是将操作转换为建图

那么对于k = 1,新增一个超级原点0 ,每个点都向他连接一条边,边权为点权 ,双向

对于k = -1,直接跑一下差分就可以了,排序完之后满足差分关系

Code:

/*** keep hungry and calm CoolGuang!***/
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define d(x) printf("%lld\n",x);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll INF= 1e17;
const ll maxn = 1e6+700;
const int mod= 1e9+7;
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
ll n,m,p;
vector<pair<ll,ll>>v[maxn];
ll dis[maxn];
int vis[maxn];
set<pair<int,int>>s;
struct node{int x,id;bool friend operator<(node a,node b){if(a.x == b.x) return a.id < b.id;return a.x < b.x;}
}q[maxn];ll spfa(){queue<int>q;for(int i=0;i<=n;i++) dis[i] = INF,vis[i] = 0;q.push(1);vis[1] = 1;dis[1] = 0;while(!q.empty()){int u = q.front();q.pop();vis[u] = 0;for(auto x:v[u]){int e = x.first,w = x.second;if(dis[e]> dis[u] + w){dis[e] = dis[u] + w;if(!vis[e]) q.push(e),vis[e] = 1;}}}return dis[n];
}
int main(){read(n);read(m);read(p);for(int i=1;i<=m;i++){int x,y,w;read(x);read(y);read(w);v[x].push_back({y,w});v[y].push_back({x,w});}for(int i=1;i<=n;i++){read(q[i].x);q[i].id = i;}///123456debug(1);sort(q+1,q+1+n);if(p == 1){for(int i=1;i<=n;i++) {v[0].push_back({q[i].id,q[i].x});v[q[i].id].push_back({0,q[i].x});}}else{for(int i=2;i<=n;i++){v[q[i].id].push_back({q[i-1].id,q[i].x-q[i-1].x});v[q[i-1].id].push_back({q[i].id,q[i].x-q[i-1].x});}}printf("%lld\n",spfa());return 0;
}
/***
4 3 1
1 2 3
2 3 1
3 4 1
1 1 1 1
***/

 

本文标签: upc山路 (ghat)