pku1847 Tram

2024-05-15 09:08
文章标签 tram pku1847

本文主要是介绍pku1847 Tram,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目大意:使switch改变尽量少。输入First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed. 除了第ith行的第一个值为0,其余都为1.求最少改变次数也就可以转换为最短路。

 

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;struct Node{int to;int cost;
};struct qnode{int v;int cost;bool operator < (const qnode &r) const{return cost > r.cost;}
};void init(vector<Node> *G, int n)
{for(int i = 0; i <= n; i ++)G[i].clear();
}int dijkstra(vector<Node> *G, int n, int src, int des)
{int vis[105];int dist[105];qnode temp;memset(vis, 0, sizeof(vis));memset(dist, inf, sizeof(dist));dist[src] = 0;priority_queue<qnode> q;temp.v = src;temp.cost = 0;//q.push((qnode){src, 0});q.push(temp);while(!q.empty()){qnode x = q.top();  q.pop();int v = x.v;if(vis[v] == 1) continue;vis[v] = 1;for(int i = 0; i < G[v].size(); i ++){if(dist[G[v][i].to] > (dist[v] + G[v][i].cost)){dist[G[v][i].to] = dist[v] + G[v][i].cost;temp.v = G[v][i].to;temp.cost = dist[G[v][i].to];//q.push((qnode){G[v][i].to, dist[G[v][i].to]});q.push(temp);}}}if(dist[des] == inf) return -1;else return dist[des];
}int main()
{int n, a, b;vector<Node> G[105];while(scanf("%d%d%d", &n, &a, &b) != EOF){init(G, n);for(int i = 1; i <= n; i ++){int m, x;Node node;scanf("%d", &m);for(int j = 0; j < m; j ++){scanf("%d", &x);node.to = x;if(j == 0)node.cost = j;else node.cost = 1;G[i].push_back(node);}}//for(int i = 1; i <= n; i ++){//   for(int j = 0; j < G[i].size(); j ++){//        printf("%d %d\n", G[i][j].to, G[i][j].cost);//    }//    printf("\n");// }int ans = dijkstra(G, n, a, b);printf("%d\n", ans);}return 0;
}


 第二次改用数组N^2实现dijkstra。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;const int inf = 0x3f3f3f3f;
int dist[105];
int vis[105];
int path[105];
int graph[105][105];int dijkstra(int n, int src, int des)
{memset(vis, 0, sizeof(vis));memset(dist, inf, sizeof(dist));dist[src] = 0;int pre = -1;//   for(int i = 1; i <= n; i ++)//       printf("%d ", dist[i]);//   printf("\n");for(int i = 1; i <= n; i ++){int Min = inf;for(int j = 1; j <= n; j ++){if(vis[j] == 0 && dist[j] < Min){Min = dist[j];pre = j;}}//     printf("~%d\n", pre);vis[pre] = 1;for(int j = 1; j <= n; j ++){if(vis[j] == 0&& dist[j] > dist[pre] + graph[pre][j]){dist[j] = dist[pre] + graph[pre][j];}}}if(dist[des] == inf) return -1;else return dist[des];}int main()
{int n, a, b;while(scanf("%d%d%d", &n, &a, &b) != EOF){memset(graph, inf, sizeof(graph));for(int i = 0; i <= n; i ++) graph[i][i] = 0;for(int i = 1; i <= n; i ++){int m, x;scanf("%d", &m);for(int j = 0; j < m; j ++){scanf("%d", &x);if(j == 0) graph[i][x] = 0;else graph[i][x] = 1;}}int ans = dijkstra(n, a, b);printf("%d\n", ans);}return 0;
}

第一种方法用时19MS,第二种方法0MS。这是杀鸡焉用牛刀么。

 


                                    

这篇关于pku1847 Tram的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/991408

相关文章

POJ 1847 Tram(Dijkstra单源有向图最短路径算法)

//Accepted 212 KB 0 ms C++ 1096 B 2013-02-27 19:42:55/*Sample Input3 2 12 2 32 3 12 1 2Sample Output0题意:给出N个站点,每个站点都有铁路通向其它的多个站点。如果当前要走的铁路是现在开关指向的铁路,则直接走即可,否则要手动扳动开关。 难理解的可能是题意:直接指向的 w = 0, 需要

POJ - 1847 Tram 最短路,思维建图

题目链接 POJ-1847 题意 给定n节点,节点之间有道路相连,但是每个节点都有个开关,只有开关指向的节点才能通行,你可以搬动开关。给定起点终点,求最少搬动开关次数。 解法 建图,对于每个节点,初始开关对准的节点连边,权值为0,代表经过这个边不需要动开关。对于其他链接的节点连边,权值为1,代表经过这个边需要动开关。跑一遍起点到终点最短路,dis数组求出来的就是最少搬动开关次数。 代码