stanford 算法 week 5

Question 1

In this programming problem you'll code up Dijkstra's shortest-path algorithm. 
Download the text file  here. (Right click and save link as). 
The file contains an adjacency list representation of an undirected weighted graph with 200 vertices labeled 1 to 200. Each row consists of the node tuples that are adjacent to that particular vertex along with the length of that edge. For example, the 6th row has 6 as the first entry indicating that this row corresponds to the vertex labeled 6. The next entry of this row "141,8200" indicates that there is an edge between vertex 6 and vertex 141 that has length 8200. The rest of the pairs of this row indicate the other vertices adjacent to vertex 6 and the lengths of the corresponding edges.

Your task is to run Dijkstra's shortest-path algorithm on this graph, using 1 (the first vertex) as the source vertex, and to compute the shortest-path distances between 1 and every other vertex of the graph. If there is no path between a vertex  v  and vertex 1, we'll define the shortest-path distance between 1 and  v  to be 1000000. 

You should report the shortest-path distances to the following ten vertices, in order: 7,37,59,82,99,115,133,165,188,197. You should encode the distances as a comma-separated string of integers. So if you find that all ten of these vertices except 115 are at distance 1000 away from vertex 1 and 115 is 2000 distance away, then your answer should be 1000,1000,1000,1000,1000,2000,1000,1000,1000,1000. Remember the order of reporting DOES MATTER, and the string should be in the same order in which the above ten vertices are given. Please type your answer in the space provided.

IMPLEMENTATION NOTES: This graph is small enough that the straightforward  O(mn)  time implementation of Dijkstra's algorithm should work fine. OPTIONAL: For those of you seeking an additional challenge, try implementing the heap-based version. Note this requires a heap that supports deletions, and you'll probably need to maintain some kind of mapping between vertices and their positions in the heap.


大家怎么都在用c++,就我一个用java的。。
用hashmap先写了一个了,又用数组写了一遍。
  1. import java.io.*;
  2. import java.util.*;

  3. public class HashTableTest {
  4.         static int[] hash=new int[10000000];
  5.         static HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
  6.         static int[] num={7, 37, 59, 82, 99, 115, 133, 165, 188, 197};
  7.         static int[] flag=new int[9];
  8.         public static void read(String filename) throws IOException{
  9.                 BufferedReader in=new BufferedReader(new FileReader(filename));
  10.                 String s;
  11.                 int i=0;
  12.                 while((s=in.readLine())!=null){
  13.                         int temp=Integer.parseInt(s);
  14.                         map.put(i,temp);
  15.                         System.out.println(temp);
  16.                         for(int j=0;j<9;j++){
  17.                                 if(map.containsValue(num[j]-temp)){
  18.                                         flag[j]=1;
  19.                                 }
  20.                         }
  21.                         i++;
  22.                 }
  23.         }
  24.         public static void read_second(String filename) throws IOException{
  25.                 BufferedReader in=new BufferedReader(new FileReader(filename));
  26.                 String s;
  27.                 int i=0;
  28.                 while((s=in.readLine())!=null){
  29.                         int temp=Integer.parseInt(s);
  30.                         System.out.println(temp);
  31.                         for(int j=0;j<9;j++){
  32.                                 if((num[j]-temp)>0&&hash[num[j]-temp]==1){
  33.                                         flag[j]=1;
  34.                                 }
  35.                         }
  36.                         hash[temp]++;
  37.                 }
  38.         }
  39.         public static void main(String[] args){
  40.                 try{read_second("dijkstraData.txt");
  41.                 for(int i=0;i<9;i++){
  42.                         System.out.print(flag[i]);
  43.                 }
  44.                 }catch(IOException err){
  45.                         System.out.println(err.toString());
  46.                         err.printStackTrace();
  47.                 }
  48.         }
  49. }
复制代码


C++的:


#include "stdafx.h"


#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;


#define UNEXPANDED 99999999
#define MAX_NODE_ID 200


struct ID_DIS
{
        int id,distance;
        bool operator < (const ID_DIS & other) const
        {
                return distance > other.distance;
        }
        ID_DIS(int p_id,int p_dis):
        id(p_id),distance(p_dis){};
};


vector<ID_DIS> adj[MAX_NODE_ID + 1];
int result[MAX_NODE_ID + 1];
priority_queue<ID_DIS> distance_heap;


int main () {
        FILE *fin  = fopen ("D://dijkstraData.txt", "r");
        FILE *fout = fopen ("D://dijkstraData_out2.txt", "w");


        int in_node,in_distance,lead_node;
        char in_char;
        while(fscanf(fin,"%d",&in_node) > 0)
        {
                fscanf(fin,"%c",&in_char);
                if(in_char != ',')
                        lead_node = in_node;
                else
                {
                        fscanf(fin,"%d",&in_distance);
                        adj[lead_node].push_back(ID_DIS(in_node,in_distance));
                }
        }


        fill(result,result + MAX_NODE_ID,UNEXPANDED);
        distance_heap.push(ID_DIS(1,0));
        while(!distance_heap.empty())
        {
                ID_DIS expanding = distance_heap.top();
                distance_heap.pop();
                if(result[expanding.id] != UNEXPANDED) continue;
                result[expanding.id] = expanding.distance;
                for(int i = 0 ; i < adj[expanding.id].size() ; i ++)
                {
                        distance_heap.push(ID_DIS(adj[expanding.id][i].id,
                                expanding.distance + adj[expanding.id][i].distance));
                }
        }


        fprintf(fout,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
                result[7],result[37],result[59],result[82],result[99],
                result[115],result[133],result[165],result[188],result[197]);
       return 0;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本系统的研发具有重大的意义,在安全性方面,用户使用浏览器访问网站时,采用注册和密码等相关的保护措施,提高系统的可靠性,维护用户的个人信息和财产的安全。在方便性方面,促进了校园失物招领网站的信息化建设,极大的方便了相关的工作人员对校园失物招领网站信息进行管理。 本系统主要通过使用Java语言编码设计系统功能,MySQL数据库管理数据,AJAX技术设计简洁的、友好的网址页面,然后在IDEA开发平台中,编写相关的Java代码文件,接着通过连接语言完成与数据库的搭建工作,再通过平台提供的Tomcat插件完成信息的交互,最后在浏览器中打开系统网址便可使用本系统。本系统的使用角色可以被分为用户和管理员,用户具有注册、查看信息、留言信息等功能,管理员具有修改用户信息,发布寻物启事等功能。 管理员可以选择任一浏览器打开网址,输入信息无误后,以管理员的身份行使相关的管理权限。管理员可以通过选择失物招领管理,管理相关的失物招领信息记录,比如进行查看失物招领信息标题,修改失物招领信息来源等操作。管理员可以通过选择公告管理,管理相关的公告信息记录,比如进行查看公告详情,删除错误的公告信息,发布公告等操作。管理员可以通过选择公告类型管理,管理相关的公告类型信息,比如查看所有公告类型,删除无用公告类型,修改公告类型,添加公告类型等操作。寻物启事管理页面,此页面提供给管理员的功能有:新增寻物启事,修改寻物启事,删除寻物启事。物品类型管理页面,此页面提供给管理员的功能有:新增物品类型,修改物品类型,删除物品类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值