hdu 3790 java_hdu3790 - 最短路径问题 (Dijkstra)(多条最短路径找花费最少的一条)...

最短路径问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 13771    Accepted Submission(s): 4234

Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。

Input 输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。

(1

Output 输出 一行有两个数, 最短距离及其花费。

Sample Input

3 2 1 2 5 6 2 3 4 5 1 3 0 0

Sample Output

9 11

Source

浙大计算机研究生复试上机考试-2010年                     这道题我竟然在INFINITY这个点wa了许久,例子:#define INFINITY 0x7fffffff    那么INFINITY + any < 0 成为一个极小的数,那么结果自然就是错的

/******************************

*

*acm: hdu-3790

*

*title: 最短路径问题

*

*time : 2014.8.29

*

*******************************/

/*

思路: 1/2

例图 ① -> ②

2/2↘ ↙1/2 (距离/花费)

可见,路径相等时,记录花费较少的即可,可直接用Dijkstra算法。

*/

#include

#include

#define MAXVEX 1001

#define MAXEDGE 100000

#define INFINITY 0x3fffffff

//不能0x7fffffff => 加上1变成负数 成了一个很小的数

//构造边集

typedef struct Edge

{

int cost; //花费

int lenth; //长度

} Edge;

Edge edges[MAXVEX][MAXVEX];

void CreateMGraph(int numVertexes, int numEdges)

{

int i, j;

int a, b;

int len, cost;

for (i = 1; i <= numVertexes; i++)

{

for (j = 1; j <= numVertexes; j++)

{

edges[i][j].lenth = INFINITY;

edges[i][j].cost = INFINITY;

}

}

for (i = 0; i < numEdges; i++)

{

scanf("%d%d%d%d", &a, &b, &len, &cost);

if (edges[a][b].lenth > len)

{

edges[a][b].lenth = len;

edges[a][b].cost = cost;

edges[b][a].lenth = len;

edges[b][a].cost = cost;

}

else if (edges[a][b].lenth == len)

{

if (edges[a][b].cost > cost)

{

edges[a][b].cost = cost;

edges[b][a].cost = cost;

}

}

}

/* 静态数据

edges[1][2].lenth = 5;

edges[1][2].cost = 6;

edges[2][1].lenth = 5;

edges[2][1].cost = 6;

edges[2][3].lenth = 4;

edges[2][3].cost = 5;

edges[3][2].lenth = 4;

edges[3][2].cost = 5;

*/

}

typedef int ShortPathTable[MAXVEX];

typedef int Cost[MAXVEX];

//Dijkstra算法

void ShortestPath_Dijkstra(int numVertexes, int numEdges, int v0, int vm, int *len, int *cost)

{

int v, w, k, min;

int min_cost;

ShortPathTable D;

int final[MAXVEX]; //标记是否求得顶点v0-vm的路径

Cost C;

*len = INFINITY;

*cost = INFINITY;

for (v = 1; v <= numVertexes; v++)

{

final[v] = 0;

D[v] = edges[v0][v].lenth;

C[v] = edges[v0][v].cost;

}

final[v0] = 1;

//开始主循环,每次求得v0到某v顶点的最短路径

for (v = 2; v <= numVertexes; v++)

{

min = INFINITY; //当前所知离v0顶点的最近距离

min_cost = 0;

for (w = 1; w <= numVertexes; w++) //寻找离v0最近的顶点

{

if (!final[w] && D[w] < min)

{

k = w;

min = D[w]; //w顶点离v0更近

min_cost = C[w];

}

}

final[k] = 1;

if (k == vm)

{

*len = D[k];

*cost = C[k];

break;

}

for (w = 1; w <= numVertexes; w++)

{

if (!final[w] && (min+edges[k][w].lenth) < D[w]) //如果经过v顶点的路径比现在这条路径的长度短的话

{

//说明找到了更短的路径,修改D[w],C[w]

D[w] = min + edges[k][w].lenth;

C[w] = min_cost + edges[k][w].cost;

}

else if (!final[w] && (min+edges[k][w].lenth) == D[w]) //如果经过v顶点的路径与现在这条路径相等

{

if (min_cost + edges[k][w].cost < C[w]) //取花费较少的值赋值

{

C[w] = min_cost + edges[k][w].cost;

}

}

}

}

}

int main()

{

int numVertexes = 3;

int numEdges = 2;

int start;

int end;

int len;

int cost;

while (scanf("%d%d", &numVertexes, &numEdges) && numVertexes != 0 && numVertexes != 0)

{

CreateMGraph(numVertexes, numEdges);

scanf("%d%d", &start, &end);

ShortestPath_Dijkstra(numVertexes, numEdges, start, end, &len, &cost);

printf("%d %d\n", len, cost);

}

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值