今天做了一道关于最短路径的算法题,虽然最后AC了,但是我写的第一个算法,我认为是没有问题的,自己编写的测试用例也全部通过,反复调试了,都没有错误。可是在OJ上一提交就提示Wrong Answer,真是苦闷啊!希望看到这篇文章的同志们给些提示。

两个算法都是用邻接表存储图的,都是比较纯粹的自定义结构体,使用的是DijkStra算法。虽然用容器也可以实现邻接表,但是个人感觉用结构体是比较简单,可以不用了解底层的实现。但是本人就是喜欢钻研一些基础的东西,无它,兴趣使然。


题目描述:
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
输入:
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点t。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
输出:
输出 一行有两个数, 最短距离及其花费。
样例输入:
3 2
1 2 5 6
2 3 4 5
1 3
0 0
样例输出:
9 11

第一个算法---为啥子错撒?

#include <iostream>
#include <malloc.h>
#define MAXVALUE 999999;
#define MAX_VERTEX_NUM 1001
#define MAX_EDGE_NUM 100001
using namespace std; struct ArcNode{
int vertex;
int len;
int cost;
ArcNode *nextarc;
}; typedef struct VertexNode{
ArcNode *firstArc;
int size;
}VNode[MAX_VERTEX_NUM]; struct Graph{
VNode nodes;
int n, e;
};
int dist[MAX_VERTEX_NUM];
int cost[MAX_VERTEX_NUM];
bool visited[MAX_VERTEX_NUM];
//Edge edges[MAX_EDGE_NUM];
void allocateArcNode(ArcNode** arcNode)
{
*arcNode = (ArcNode*)malloc(sizeof(ArcNode));
(*arcNode)->nextarc = NULL;
} void initeGraph(Graph &graph)
{
for (int i = ; i <= MAX_VERTEX_NUM; i++)
{
graph.nodes[i].firstArc = NULL;
graph.nodes[i].size = ;
} graph.n = ;
graph.e = ;
} void createGraph(Graph &graph, int m)
{
int v1, v2, len, cost;
ArcNode* p1 = NULL;
ArcNode* p2 = NULL;
for (int i = ; i <= m; i++)
{
allocateArcNode(&p1);
allocateArcNode(&p2);
cin >> v1 >> v2 >> len >> cost;
p1->vertex = v2;
p1->len = len;
p1->cost = cost;
p1->nextarc = graph.nodes[v1].firstArc;
graph.nodes[v1].firstArc = p1;
graph.nodes[v1].size++;
p2->vertex = v1;
p2->len = len;
p2->cost = cost;
p2->nextarc = graph.nodes[v2].firstArc;
graph.nodes[v2].firstArc = p2;
graph.nodes[v2].size++;
}
} //void printGraph(const Graph& graph, int n)
//{
// for (int i = 1; i <= n; i++)
// {
// ArcNode* arcPtr = graph.nodes[i].firstArc;
//
// cout << i << ": ";
// cout << "size:" << graph.nodes[i].size << endl;
// while (arcPtr != NULL)
// {
// cout << arcPtr->len << ' ' << arcPtr->cost << ";";
// arcPtr = arcPtr->nextarc;
// }
//
// cout << endl;
// }
//} void dijkStra(const Graph &graph, int v0, int n)
{
int index = v0;
for (int i = ; i <= n; i++)
{
ArcNode *arcPtr = graph.nodes[index].firstArc;
while (arcPtr != NULL)
{
int tmp = arcPtr->vertex;
int len = arcPtr->len;
int co = arcPtr->cost;
if (visited[tmp] == false || dist[tmp] > dist[index] + len || (dist[tmp] == dist[index] + len && cost[tmp] > cost[index] + co))
{
dist[tmp] = arcPtr->len;
cost[tmp] = arcPtr->cost;
}
arcPtr = arcPtr->nextarc;
}
int minLen = MAXVALUE;
int minCost = MAXVALUE;
for (int j = ; j <= n; j++)
{
if (visited[j] == false && (dist[j] < minLen || (dist[j] == minLen && cost[j] < minCost)))
{
minLen = dist[j];
index = j;
}
}
visited[index] = true;
}
} void freeSpaceOfGraph(Graph& graph)
{
for (int i = ; i <= graph.n; i++)
{
ArcNode *arcPtr = graph.nodes[i].firstArc;
while (arcPtr != NULL)
{
ArcNode *tmp = arcPtr;
arcPtr = arcPtr->nextarc;
delete tmp;
}
}
} int main()
{
int n, m; while (cin >> n >> m)
{
if (n == && m == )
break;
int start, end; for (int i = ; i <= n; i++)
{
visited[i] = false;
cost[i] = MAXVALUE;
dist[i] = MAXVALUE;
} Graph graph;
initeGraph(graph);
graph.n = n;
graph.e = m;
createGraph(graph, m);
cin >> start >> end;
dijkStra(graph, start, n);
cout << dist[end] << ' ' << cost[end] << endl;
freeSpaceOfGraph(graph);
}
return ;
}

第二算法---完美AC

#include <iostream>
#include <malloc.h>
#define MAXVALUE 123123123
#define MAX_VERTEX_NUM 1001
#define MAX_EDGE_NUM 100001
using namespace std; struct ArcNode{
int vertex;
int len;
int cost;
ArcNode *nextarc;
}; typedef struct VertexNode{
ArcNode *firstArc;
int size;
}VNode[MAX_VERTEX_NUM]; struct Graph{
VNode nodes;
int n, e;
};
int dist[MAX_VERTEX_NUM];
int cost[MAX_VERTEX_NUM];
bool visited[MAX_VERTEX_NUM];
//Edge edges[MAX_EDGE_NUM];
void allocateArcNode(ArcNode** arcNode)
{
*arcNode = (ArcNode*)malloc(sizeof(ArcNode));
(*arcNode)->nextarc = NULL;
} void initeGraph(Graph &graph)
{
for (int i = ; i <= MAX_VERTEX_NUM; i++)
{
graph.nodes[i].firstArc = NULL;
graph.nodes[i].size = ;
} graph.n = ;
graph.e = ;
} void createGraph(Graph &graph, int m)
{
int v1, v2, len, cost;
ArcNode* p1 = NULL;
ArcNode* p2 = NULL;
for (int i = ; i <= m; i++)
{
allocateArcNode(&p1);
allocateArcNode(&p2);
cin >> v1 >> v2 >> len >> cost;
p1->vertex = v2;
p1->len = len;
p1->cost = cost;
p1->nextarc = graph.nodes[v1].firstArc;
graph.nodes[v1].firstArc = p1;
graph.nodes[v1].size++;
p2->vertex = v1;
p2->len = len;
p2->cost = cost;
p2->nextarc = graph.nodes[v2].firstArc;
graph.nodes[v2].firstArc = p2;
graph.nodes[v2].size++;
}
} //void printGraph(const Graph& graph, int n)
//{
// for (int i = 1; i <= n; i++)
// {
// ArcNode* arcPtr = graph.nodes[i].firstArc;
//
// cout << i << ": ";
// cout << "size:" << graph.nodes[i].size << endl;
// while (arcPtr != NULL)
// {
// cout << arcPtr->len << ' ' << arcPtr->cost << ";";
// arcPtr = arcPtr->nextarc;
// }
//
// cout << endl;
// }
//} void dijkStra(const Graph &graph, int v0, int n)
{
int index = v0;
dist[v0] = ;
cost[v0] = ;
visited[v0] = true;
for (int i = ; i <= n; i++)
{
ArcNode *arcPtr = graph.nodes[index].firstArc;
while (arcPtr != NULL)
{
int tmp = arcPtr->vertex;
int len = arcPtr->len;
int co = arcPtr->cost;
if ((visited[tmp] == false) && (dist[tmp] > dist[index] + len
|| (dist[tmp] == dist[index] + len && cost[tmp] > cost[index] + co)))
{
dist[tmp] = dist[index] + len;
cost[tmp] = cost[index] + co;
}
arcPtr = arcPtr->nextarc;
}
int minLen = MAXVALUE;
int minCost = MAXVALUE;
for (int j = ; j <= n; j++)
{
if (visited[j] == true || dist[j] == MAXVALUE)
continue;
if (dist[j] < minLen || (dist[j] == minLen && cost[j] < minCost))
{
minLen = dist[j];
minCost = cost[j];
index = j;
}
}
visited[index] = true;
}
} void freeSpaceOfGraph(Graph& graph)
{
for (int i = ; i <= graph.n; i++)
{
ArcNode *arcPtr = graph.nodes[i].firstArc;
while (arcPtr != NULL)
{
ArcNode *tmp = arcPtr;
arcPtr = arcPtr->nextarc;
delete tmp;
}
}
} int main()
{
int n, m; while (cin >> n >> m)
{
if (n == && m == )
break;
int start, end; for (int i = ; i <= n; i++)
{
visited[i] = false;
cost[i] = MAXVALUE;
dist[i] = MAXVALUE;
} Graph graph;
initeGraph(graph);
graph.n = n;
graph.e = m;
createGraph(graph, m);
cin >> start >> end;
dijkStra(graph, start, n);
cout << dist[end] << ' ' << cost[end] << endl;
freeSpaceOfGraph(graph);
}
return ;
} /**************************************************************
Problem: 1008
User: 凌月明心
Language: C++
Result: Accepted
Time:10 ms
Memory:1528 kb
****************************************************************/

终于AC了“最短路径”的更多相关文章

  1. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  2. NYOJ-79 拦截导弹 AC 分类: NYOJ 2014-01-01 23:25 167人阅读 评论(0) 收藏

    #include<stdio.h> int main(){ int num[1000]={0}; int n,m,x,y; scanf("%d",&n); wh ...

  3. hdu 4878 ZCC loves words AC自动机+中国剩余定理+快速幂

    题意就不说了. 分析:折腾好几天自己写的代码还是看了别人代码后发现几乎没什么复杂度的差别,可是就是一直超时,后来干脆照着别人写啊,一直WA,就在准备放弃干脆先写这篇博客的时候,又看了一眼WA的代码,发 ...

  4. 记一个问题的AC

    今天突然做一道LCT的染色问题的时候突然想到一个两个月前一道没有AC的题目. 链接 大意是,给一个长度为10^4的序列,最多有255个不同的数字,有最多10^5次方个询问,对于每个询问 l,r 输出[ ...

  5. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

  6. 最短路径问题的Dijkstra算法

      问题 最短路径问题的Dijkstra算法 是由荷兰计算机科学家艾兹赫尔·戴克斯特拉提出.迪科斯彻算法使用了广度优先搜索解决非负权有向图的单源最短路径问题,算法终于得到一个最短路径树>    ...

  7. 【BZOJ】1030: [JSOI2007]文本生成器(AC自动机+dp)

    题目 传送门:QWQ 传送到洛谷QWQ 分析 我一开始也不会做这题的,后来看了很多网上的题解,终于AC了.(我好菜啊) 主要参考:传送门QWQ 直接搞非常麻烦,反正我是不会做.于是考虑求反,即求有多少 ...

  8. NOIP2014提高组 DAY1 -SilverN

    T1  生活大爆炸版石头剪刀布 题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的 ...

  9. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

随机推荐

  1. spring中action和url的对应关系

    spring 中, action和url的对应关系             在web.xml中,这样配置:           <servlet-mapping >             ...

  2. 编写自己的jquery插件

    如何编写自己的jquery插件 Jquery的插件主要分为三类: .封装对象方法的插件:大部分插件都是封装对象的插件 .封装全局函数的插件:将独立的函数添加到jquery的命名空间之下.Jquery. ...

  3. (部署)使用kubernetes的deployment进行RollingUpdate

    rolling update,可以使得服务近乎无缝地平滑升级,即在不停止对外服务的前提下完成应用的更新. replication controller与deployment的区别 replicatio ...

  4. Oracle学习笔记之二,Oracle 11g体系结构

    Oracle 11g体系结构概述 实例(Instance),是指一组Oracle后台进程以及在服务器中分配的共享内存区域: 数据库(Database),是由基于磁盘的数据文件.控制文件.日志文件.参数 ...

  5. Android中的httpclient框架发送get请求

    /** * 採用httpclientGet请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到 ...

  6. 【Android】3.15 短串分享功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 短串分享是指,用户搜索查询后得到的每一个地理位置结果将会对应一条短串(短链接),用户可以通过短信.邮 ...

  7. linux服务器文件删除空间却未释放

    在Linux或者Unix系统中,通过rm或者文件管理器删除文件将会从文件系统的目录结构上解除链接(unlink),然而如果文件是被打开的(有一个进程正在使用),那么进程将仍然可以读取该文件,磁盘空间也 ...

  8. static_cast、dynamic_cast、reinterpret_cast、和const_cast

    关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++ 之父的<C++ 的设计和演化>.最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_ ...

  9. nyoj311 完全背包 经典背包问题

    完全背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 直接说题意,完全背包定义有N种物品和一个容量为V的背包,每种物品都有无限件可用.第i种物品的体积是c,价值是w. ...

  10. python URLObject url处理模块

    1.需求来源 给一个url串,例如https://github.com/zacharyvoase/urlobject?spam=eggs#foo,想要截取串中某个部分,比如传输协议(https).服务 ...