HDU 4725

The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9444    Accepted Submission(s): 2098


Problem Description
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.
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.
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.
 

Input
The first line has a number T (T <= 20) , indicating the number of test cases.
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.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N 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.
 

Output
For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.
 

Sample Input
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
 
Sample Output
Case #1: 2 Case #2: 3

题意:给你点和每个点所在的层,层和层之间移动有花费

方法1:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e5+5;
int n,m,c,l;
int head[maxn],point[maxn],vis[maxn],d[maxn];
vector<int> dc[maxn];
struct ac{
    int to,next,value;
}eg[2*maxn];
void add(int u,int v,int w){
    eg[l].to=v;
    eg[l].value=w;
    eg[l].next=head[u];
    head[u]=l++;
}
struct ak{
    bool operator()(const int &a,const int &b){ //优先队列排序规则
        return d[a]>d[b];
    }
};
void dijkstra(int star){
    memset(vis,0,sizeof(vis));
	memset(d,INF,sizeof(d));
    priority_queue<int, vector<int>, ak> q;
    d[star]=0;
    q.push(star);
    while(!q.empty()){
        int x=q.top();q.pop();
        for(int k=head[x];k!=-1;k=eg[k].next){
            int y=eg[k].to;
            if(d[y]>d[x]+eg[k].value){
                d[y]=d[x]+eg[k].value;
                q.push(y);
            }
        }
        if(point[x]<n&&!vis[point[x]+1]){ //判断要不往上走一层
            vis[point[x]+1]=1;
            for(int i=0;i<dc[point[x]+1].size();i++){
                int y=dc[point[x]+1][i];
                if(d[y]>d[x]+c){
                    d[y]=d[x]+c;
                    q.push(y);
                }
            }
        }
        if(point[x]>1 &&!vis[point[x]-1]){ //判断要不要往下走一层
            vis[point[x]-1]=1;
            for(int i=0;i<dc[point[x]-1].size();i++){
                int y=dc[point[x]-1][i];
                if(d[y]>d[x]+c){
                    d[y]=d[x]+c;
                    q.push(y);
                }
            }
        }
    }

}
int main(){
    int t,f=0;
    scanf("%d",&t);
    while(t--){
        l=0;
        scanf("%d %d %d",&n,&m,&c);
        memset(head,-1,sizeof(head));
        memset(point,0,sizeof(point));
        int u,v,w;
        for(int i=1;i<=n;i++){
            scanf("%d",&u);
            point[i]=u; //第i个点在第u层
            dc[u].push_back(i); //第u层有i点
        }
        for(int i=0;i<m;i++){
            scanf("%d %d %d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        dijkstra(1);
        f++;
        printf("Case #%d: ", f);
        if(d[n]==INF||n==0) printf("-1\n");
        else printf("%d\n",d[n]);
        for(int i = 1; i <= n; ++i) dc[i].clear();
    }
    return 0;
}

方法2:

把点和层相连,点和层的距离为0,把层当成一个新的点

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 5 * 1e5;
int n, m, c;
int pos[maxn];
vector<int> laye[maxn];
int head[maxn];
int d[maxn];
bool vis[maxn];
struct edge{
    int to, nex, cost;
}eg[maxn];
int cnt = 0;
void add(int u,int v,int cost) {
    eg[cnt].to = v;
    eg[cnt].nex = head[u];
    eg[cnt].cost = cost;
    head[u] = cnt++;
}
struct Rule {
    bool operator () (int a,int b) const {
        return d[a] > d[b];
    }
};
void dijkstra(int s) {
    memset(d, inf, sizeof(d));
    priority_queue<int, vector<int>, Rule> q;
    d[s] = 0;
    q.push(s);
    while(!q.empty()) {
        int u = q.top(); q.pop();
        for(int k = head[u]; k != -1; k = eg[k].nex) {
            int v = eg[k].to;
            if(d[v] > d[u] + eg[k].cost) {
                //   printf("%d %d %d\n", u, v,eg[k].cost);
                d[v] = d[u] + eg[k].cost;
                q.push(v);
            }
            
        }
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    for(int cas = 1; cas <= t; ++cas) {
        
        cnt = 0;
        memset(d, inf, sizeof(d));
        memset(head, -1, sizeof(head));
        memset(pos, 0, sizeof(pos));
        scanf("%d %d %d", &n, &m, &c);
        int u, v, cost;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &u);
            pos[i] = u;
            add(i, n + 2 * u - 1, 0);//层内的点到该层距离为0
            add(n + 2 * u, i, 0);
            vis[u] = true;
        }
        for(int i = 1; i < n; ++i) {
            if(vis[i] && vis[i + 1]) {
                add(n + 2 * i - 1, n + 2 * (i + 1), c);//相邻层之间的距离为c
                add(n + 2 * (i + 1) - 1, n + 2 * i, c);
            }
        }
        for(int i =0; i < m; ++i) {
            scanf("%d %d %d", &u, &v, &cost);
            add(u, v, cost);
            add(v, u, cost);
        }
        
        dijkstra(1);
        if(d[n] == inf || n == 0)
            printf("Case #%d: -1\n", cas);
        else
            printf("Case #%d: %d\n", cas, d[n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值