lanqiao OJ 2194 出差 bellman—ford

简介: lanqiao OJ 2194 出差 bellman—ford

用户登录

单源最短路,可以存在负数  

#include<iostream>
#include<algorithm>
#include<cstring>
 
using namespace std ;
const int INF = 0x3f3f3f3f ;
const int N = 20010 ;
struct edge{
  int a , b , c ;
}e[N];
int t[N] ;
int dis[N] ;
int n , m ;
int main(){
  cin >> n >> m ;
  for(int i = 1 ; i <= n ;i ++) cin >> t[i] ;
  for(int i = 1 ; i <= m ; i ++){
    int a, b ,c ;
    cin >> a >> b >> c ;
    e[i].a = a , e[i].b = b , e[i].c = c ;
    e[i+m].a = b , e[i+m].b = a , e[i+m].c = c ; 
  }
  memset(dis,INF ,sizeof(dis)) ;
  dis[1] = 0 ;
  for(int k = 1 ; k <= n ; k ++){
    for(int i = 1 ; i <= 2*m ; i ++){
      int u = e[i].a , v = e[i].b ;
      int res = t[v] ;
      if(v == n) res = 0 ;
      dis[v] = min(dis[v],dis[u] + res + e[i].c) ;
    }
  }
  cout << dis[n] << endl ;
}
目录
相关文章
|
3天前
lanqiao OJ 22年省赛 扫雷
lanqiao OJ 22年省赛 扫雷
12 1
|
3天前
lanqiao OJ 1447 砝码称重
lanqiao OJ 1447 砝码称重
12 1
|
3天前
lanqiao OJ 1505 剪邮票
lanqiao OJ 1505 剪邮票
9 0
|
3天前
lanqiao OJ 689 四阶幻方
lanqiao OJ 689 四阶幻方
9 0
|
3天前
lanqiao OJ 99 分巧克力
lanqiao OJ 99 分巧克力
8 1
|
1天前
lanqiao oj 1121 蓝桥公园(floyd)
lanqiao oj 1121 蓝桥公园(floyd)
6 0
|
1天前
lanqiao OJ 207 大臣的旅费(两次dfs)
lanqiao OJ 207 大臣的旅费(两次dfs)
6 0
|
1天前
lanqiao oj 186 糖果(状态压缩dp)
lanqiao oj 186 糖果(状态压缩dp)
4 0
|
2天前
lanqiao OJ 2097 青蛙过河
lanqiao OJ 2097 青蛙过河
6 0
|
1天前
lanqiao oj 1135 蓝桥幼儿园(并查集)
lanqiao oj 1135 蓝桥幼儿园(并查集)
5 0