hdu4725The Shortest Path in Nya Graph---spfa求最短路

hdu4725The Shortest Path in Nya Graphhttp://acm.hdu.edu.cn/showproblem.php?pid=4725

The Shortest Path in Nya Graph

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


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 <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), 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
 

Source


题意:这个题意也是相当的费劲......有n个点,有n层楼,每个点对应一个楼层相邻楼层之间要花费C;另外有m条路,每条路都有相应的花费.问从点1到点n的最小花费.

思路:参考了许多大神的思路,对于把每个点拆分那样的做法,个人不是很理解.......

将同在一个楼层里的点存起来,在用spfa处理的时候对相邻的楼层也进行入队处理.....就是这样.

但是需要注意的是在存储楼层的点的时候得在建边时就存,然后再对每个楼层扫一遍,不然会超时的.....,然后对楼层里没有元素的加入对应的点,不然会wa....

还有就是真的不能在用vector来建边了,真的会超时..........这些都是血的教训....

参考了大神的代码(好吧,,,算是复制吧).........

http://blog.csdn.net/madrishing/article/details/11626163

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>

using namespace std;

#define INF 0x3f3f3f3f
const int MAXN = 100000+100;
int n,m,c,cnt = 0;

struct Node
{
    int v,c,next;
} node[MAXN*5];
int head[MAXN];
void addedge(int u,int v,int w)
{
    node[cnt].v = v;
    node[cnt].c = w;
    node[cnt].next = head[u];
    head[u] = cnt++;
}
vector<int>Floor[MAXN];
bool vis[MAXN];
int dis[MAXN],law[MAXN];

void spfa()
{
    memset(vis,false,sizeof(vis));
    for(int i = 1; i <= n; i++)dis[i] = INF;
    vis[1] = true;
    queue<int>q;
    q.push(1);
    dis[1] = 0;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        vis[x] = false;
        for(int i = head[x]; i != -1; i = node[i].next)
        {
            int v = node[i].v;
            int cost = node[i].c;
            if(dis[v] > dis[x] + cost)
            {
                dis[v] = dis[x] + cost;
                if(!vis[v])
                {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
        int k;
        if(law[x] > 1)
        {
            int length = Floor[k = (law[x] - 1)].size();
            for(int i = 0; i < length; i++)
            {
                int v = Floor[k][i];
                if(dis[v] > dis[x] + c)
                {
                    dis[v] = dis[x] + c;
                    if(!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        if(law[x] < n)
        {
            int length = Floor[k = (law[x] + 1)].size();
            for(int i = 0; i < length; i++)
            {
                int v = Floor[k][i];
                if(dis[v] > dis[x] + c)
                {
                    dis[v] = dis[x] + c;
                    if(!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
    }
}

int main()
{
//    freopen("in.txt","r",stdin);
    int t,ca = 1;
    scanf("%d",&t);
    while(t --)
    {
        cnt = 0;
        memset(vis,false,sizeof(vis));
        memset(head,-1,sizeof(head));
        scanf("%d%d%d",&n,&m,&c);
        for(int i = 1; i <= n; i++)
            scanf("%d",&law[i]),Floor[i].clear();
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
            if(!vis[u])
            {
                vis[u] = true;
                Floor[law[u]].push_back(u);
            }
            if(!vis[v])
            {
                vis[v] = true;
                Floor[law[v]].push_back(v);
            }
        }
        if(!vis[1])Floor[law[1]].push_back(1);
        if(!vis[n])Floor[law[n]].push_back(n);
        for(int i = 1; i <= n; i++)
            if(Floor[law[i]].empty())
                Floor[law[i]].push_back(i);
        spfa();
        printf("Case #%d: ",ca++);
        if(dis[n] >= INF)printf("-1\n");
        else printf("%d\n",dis[n]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值