[kuangbin]专题四 最短路练习 The Shortest Path in Nya Graph HDU - 4725【dijkstra】

【题目描述】
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
第一句不知道在说什么。如果你不理解这一段的话,那就继续吧。
The Nya graph is an undirected graph with “layers”. Each node in the graph belongs to a layer, there are N nodes in total.
Nya图是具有“层”的无向图。 图中的每个节点都属于一个层,总共有N个节点。
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
您可以从层x中的任何节点移动到层x + 1中的任何节点,成本为C,因为道路是双向的,从层x + 1移动到层x也允许具有相同的成本。
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.
此外,还有M个额外边缘,每个边缘连接一对节点u和v,成本为w。
帮助我们计算从节点1到节点N的最短路径。

【输入】
The first line has a number T (T <= 20) , indicating the number of test cases.
第一行的数字为T(T <= 20),表示测试用例的数量。
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
对于每个测试用例,第一行有三个数字N,M(0 <= N,M <= 105)和C(1 <= C <= 103),这是节点数,额外边数 和相邻层之间移动的成本。
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
第二行具有N个数字l i(1 <= l i <= N),这是第i个节点所属的层。
Then come M lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.
然后得到M行,每行有3个数字,u,v(1 <= u,v <= N,u <> v)和w(1 <= w <= 104),这意味着有一条额外的边,连接 一对节点u和v,成本为w。

【输出】
For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
对于测试用例X,首先输出“Case #X:”,然后输出从节点1移动到节点N的最小成本。
If there are no solutions, output -1.
如果没有解决方案,则输出-1。

【样例输入】
2
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3

3 3 3
1 3 2
1 2 2
2 3 2
1 3 4

【样例输出】
Case #1: 2
Case #2: 3

题目链接:https://cn.vjudge.net/problem/HDU-4725

建图有点麻烦,
学习了bin巨的思路

第i层到第i+1层
点从n+2*i-1入
经n+2*i到n+2*(i+1)-1

回来的时候
点从n+2*(i+1)-1入
经n+2*(i+1)到n+2*i-1

代码如下:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=3*100000;
struct Node{
    int v,c;
    Node(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator < (const Node &r) const {
        return c>r.c;
    }
};
struct Edge{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN+10];
bool vis[MAXN+10];
int dist[MAXN+10];
void dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        dist[i]=INF;
        vis[i]=false;
    }
    priority_queue<Node> Q;
    dist[start]=0;
    Q.push(Node(start,0));
    while(!Q.empty())
    {
        Node tmp=Q.top();
        Q.pop();
        int u=tmp.v;
        if(vis[u])
            continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            int cost=E[u][i].cost;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                Q.push(Node(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    for(int kase=1;kase<=T;kase++)
    {
        int n,m,c;
        cin>>n>>m>>c;
        for(int i=1;i<=3*n;i++)
            E[i].clear();
        int tmp;
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            addedge(i,n+2*tmp-1,0);
            addedge(n+2*tmp,i,0);
        }
        int u,v,w;
        for(int i=1;i<=m;i++)
        {
            cin>>u>>v>>w;
            addedge(u,v,w);
            addedge(v,u,w);
        }
        for(int i=1;i<n;i++)
        {
            addedge(n+2*i-1,n+2*(i+1),c);
            addedge(n+2*(i+1)-1,n+2*i,c);
        }
        dijkstra(3*n,1);
        cout<<"Case #"<<kase<<": ";
        if(dist[n]==INF)
            cout<<-1<<endl;
        else
            cout<<dist[n]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值