您的位置:首页 > 其它

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

2016-03-12 22:43 387 查看
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 <= 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

 

Source

2013 ACM/ICPC Asia Regional Online —— Warmup2

题意:这个题意也是相当的费劲......有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
)Floor[law
].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
>= INF)printf("-1\n");
else printf("%d\n",dis
);
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: