ZOJ - 2750 Idiomatic Phrases Game (最短路径)
来源:互联网 发布:英雄无敌3死亡阴影mac 编辑:程序博客网 时间:2024/04/29 18:24
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1750
题意:
n个成语,前一个成语的最后一个字,与后一个成语的第一个字相同,求最小查询时间
分析:
有向图,查询时间作为权值,Dijkstra算法
代码:
#include <stdio.h>#include <iostream>#include <string.h>#include <string>#include <math.h>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define MAX 1000#define MIN -1e+10#define INF 0x7f7f7f7fint t, n, m;int Edge[MAX][MAX];int s[MAX]; // 已达点 - 未达点int dist[MAX]; // 由起点出发,到达i的最小距离void Dijkstra(){int i, j;for(i = 1; i<=n; i++) // 起点为1{s[i] = 0;dist[i] = Edge[1][i];}s[1] = 1;dist[1] = 0;for(i = 1; i<n; i++){int mint = INF, v = -1;for(j = 1; j<=n; j++) // 找出当前,由已达点到未达点的最小距离{if(!s[j] && dist[j] < mint){mint = dist[j];v = j;}}if(v == -1) break;s[v] = 1;for(j = 1; j<=n; j++) // 维护更新已达点到未达点的距离{if(!s[j] && dist[j] > Edge[v][j] + dist[v]){dist[j] = Edge[v][j] + dist[v];}}}printf("%d\n", dist[n]<INF?dist[n]:-1);}struct Node{int w;char begin[5], end[5];}a[1000+10];int main(){int i, j;freopen("a.txt", "r", stdin);while(~scanf("%d", &n) && n){char str[1000+10];for(i = 1; i<=n; i++){scanf("%d%s", &a[i].w, str);strncpy(a[i].begin, str, 4);a[i].begin[4] = '\0';strcpy(a[i].end, str+strlen(str) - 4);}memset(Edge, INF, sizeof(Edge));for(i = 1; i<=n; i++){for(j = 1; j<=n; j++){if(strcmp(a[i].end, a[j].begin) == 0){Edge[i][j] = a[i].w;}}}Dijkstra();}return 0;}
0 0
- ZOJ - 2750 Idiomatic Phrases Game (最短路径)
- zoj 2750 Idiomatic Phrases Game(最短路径)
- HDOJ 题目1546 Idiomatic Phrases Game(最短路径)
- zoj 2750 Idiomatic Phrases Game(最短路)
- zoj 2750 Idiomatic Phrases Game 最短路
- HDU1546 Idiomatic Phrases Game 最短路径(Dijkstra算法)
- zoj 2750 Idiomatic Phrases Game
- zoj 2750 Idiomatic Phrases Game
- ZOJ-2750- Idiomatic Phrases Game
- ZOJ 2750 Idiomatic Phrases Game
- zoj 2750 Idiomatic Phrases Game
- ZOJ:2750 Idiomatic Phrases Game
- zoj 2750 Idiomatic Phrases Game
- zoj 2750 Idiomatic Phrases Game
- 杭电 hdu 1546 Idiomatic Phrases Game (最短路径 + Dijkstra)
- HDU 1546 Idiomatic Phrases Game(最短路径)DIJ算法
- ZOJ 2750 Idiomatic Phrases Game(最短路,dij)
- ZOJ 2750 Idiomatic Phrases Game 最短路(dijkstra)
- IO流(RandomAccessFile)
- 基于Android小说阅读器滑动效果的一种实现
- IO流(操作基本数据类型的流对象DataStream)
- HDU 2063 过山车(匈牙利算法 二分图的最小顶点覆盖 二分图最大匹配)
- ZMQ简介
- ZOJ - 2750 Idiomatic Phrases Game (最短路径)
- IO流(ByteArrayStream)
- codeforces memsql Start[c]UP 2.0 C. Magic Trick
- Linux下C/C++语言的调试 gdb工具 笔记
- IO流(转换流的字符编码)
- Linux KDB 调试1
- spring中的设计模式-单例模式
- 手机游戏服务器使用UDP还是TCP
- 字符编码