zoukankan      html  css  js  c++  java
  • 【vijos】1746 小D的旅行(dijkstra)

    https://vijos.org/p/1746

    这题就是水题。裸的跑完每个点的最短路后直接可以暴力出解。。

    这题贴出来是因为我改了下我的dijkstra的模板。。。(其实是原来一直写错了233

    注意vis不要提前加。否则你懂的。。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << (#x) << " = " << (x) << endl
    #define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
    #define printarr1(a, b) for1(_, 1, b) cout << a[_] << '	'; cout << endl
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    
    #define mkpii(a, b) make_pair<int, int> (a, b)
    const int N=305, oo=(~0u>>2)-10005;
    typedef pair<int, int> pii;
    int d[N][N], n, m, ihead[N], cnt, v[N], ans=oo, vis[N];
    struct ED { int next, to, w; }e[N*N<<1];
    priority_queue<pii, vector<pii>, greater<pii> > q;
    void add(int u, int v, int w) {
    	e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
    	e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].w=w;
    }
    void dij(int s, int *d) {
    	for1(i, 1, n) d[i]=oo, vis[i]=0; int y;
    	while(!q.empty()) q.pop();
    	d[s]=0; q.push(mkpii(0, s));
    	while(!q.empty()) {
    		int x=q.top().second; q.pop();
    		if(vis[x]) continue; vis[x]=1;
    		for(int i=ihead[x]; i; i=e[i].next) if(d[y=e[i].to]>d[x]+e[i].w) {
    			d[y]=d[x]+e[i].w;
    			q.push(mkpii(d[y], y));
    		}
    	}
    }
    int main() {
    	read(n); read(m);
    	for1(i, 1, n) read(v[i]);
    	for1(i, 1, m) {
    		int u=getint(), v=getint(), w=getint();
    		add(u, v, w);
    	}
    	for1(i, 1, n) dij(i, d[i]);
    	read(m);
    	while(m--) {
    		int x=getint(), y=getint();
    		ans=oo;
    		for1(i, 1, n) ans=min(ans, d[i][x]+d[i][y]+v[i]);
    		if(ans==oo) ans=-1;
    		printf("%d
    ", ans);
    	}
    	return 0;
    }
    

      


    描述

    旅行是一件颇有趣的事情,但是在旅行前规划好路线也很重要。

    现在小D计划要去U国旅行。

    U国有N个城市,M条道路,每条道路都连接着两个城市,并且经过这条道路需要一定的费用wi。

    现在小D想要从u城市到v城市,但是他的汽车需要在途中加一次油(途中包括u和v两个城市)。在每个城市加油都有不同的费用vi。

    小D想知道从u城市到v城市最少需要多少费用(经过道路的费用+加油的费用)。

    城市从1-n进行编号。

    格式

    输入格式

    第一行两个正整数n,m,表示n个城市,m条无向道路
    接下来n行,第i行一个整数vi,表示第i个城市的加油费用
    接下来m行,第i行三个整数ai, bi, wi,表示第i条道路连接ai和bi两个城市,经过要花费wi的费用
    接下来一个正整数q,表示小D有q个询问
    接下来q行,第i行两个整数ui, vi, 表示小D想知道从ui到vi需要的最少费用(ui和vi可能相等)

    输出格式

    对于每个询问,输出一行整数,表示最小的费用,如果ui不能到达vi,则输出-1

    样例1

    样例输入1[复制]

     
    3 6
    2666
    3977
    2457
    1 2 6920
    1 2 276
    1 3 839
    3 1 3490
    2 1 7395
    3 1 7540
    6
    3 2
    3 1
    2 2
    2 1
    3 2
    2 2

    样例输出1[复制]

     
    3572
    3296
    3218
    2942
    3572
    3218

    限制

    每个测试点1s

    提示

    对于30%的数据,保证n<=10
    对于70%的数据,保证n<=80
    对于100%的数据,保证n<=300
    保证q,m<=n*n, 0 <= wi, vi <= 10000
    数据中可能有重边和自环

  • 相关阅读:
    ios 手写键盘闪退问题 UIKBBlurredKeyView candidateList
    ios NSURLErrorDomain Code=-1004 "未能连接到服务器。问题
    最牛B的编码套路
    watchOS开发—初步认识
    iOS开发拓展篇—蓝牙之CoreBlueTooth(BLE)
    iOS开发拓展篇—蓝牙之mutipeerConnectivity的使用
    iOS开发拓展篇—蓝牙之GameKit使用
    iOS开发拓展篇—ReactiveCocoa常见操作方法介绍(进阶篇)
    iOS开发拓展篇—ReactiveCocoa介绍(基础篇)
    iOS开发拓展篇—异常处理
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/4025463.html
Copyright © 2011-2022 走看看