高手帮忙编个题~小弟不会~万分跪求!!!!!

conan19870811 2008-09-07 12:04:08
题目:The K-th City
Given a map of your country, there are N cities. The cities are labeled as 0, 1, ..., N - 1, and you live in city 0. Can you calculate out the K-th nearest city form you? If two or more cities have the same distance form you, you may assume that the city with smaller label is nearer than the city with bigger one.
Input Format: (a5.in)
There are several cases. The first line of each case is two integers N and M (1 ≤ N ≤ 200, 0 ≤ M ≤ 10000), which is the number of cities in your country and the total number of roads in your country. There are three integers in each of the following M lines, A, B, C, which descript one road. A and B are the two cities that connected by that road, and C is the length of that road (1 ≤ C ≤ 2000). The roads are of both directions, and no two roads connect two same cities. There is at least one path between any two cities. At the last line of each case is a single integer K (1 ≤ K < N).
The last case is followed by a line with a single 0.
Output Format: (a5.out)
Print the label of the K-th nearest city.
Sample Input:
4 3
0 1 120
0 2 180
1 3 40
3
4 3
0 1 120
0 3 60
3 2 30
1
0
Sample Output:
2
3
...全文
304 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-09-09
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <limits>

using namespace std;

int kthcity(int ncity, int nroad, int roads[], int k)
{
typedef vector <pair <int, int> > roads_t;
typedef map <int, roads_t> full_map_t;
typedef map <int, int> solved_t;
full_map_t full_map;
solved_t solved;
int solved_count = 0;
// prepare map
for(int i = 0; i < nroad; i++) {
int end1 = roads[i*3];
int end2 = roads[i*3+1];
int length = roads[i*3+2];
if(full_map.count(end1) > 0) {
full_map[end1].push_back(make_pair(length, end2));
}
else {
roads_t roads;
roads.push_back(make_pair(length, end2));
full_map[end1] = roads;
}
if(full_map.count(end2) > 0) {
full_map[end2].push_back(make_pair(length, end1));
}
else {
roads_t roads;
roads.push_back(make_pair(length, end1));
full_map[end2] = roads;
}
}
for(full_map_t::iterator it = full_map.begin(); it != full_map.end(); it++) {
sort(it->second.begin(), it->second.end());
}
// search
solved[0] = 0;
int min_city = 0;
int min_path = 0;
while(solved_count < k) {
min_path = INT_MAX;
min_city = -1;
for(solved_t::const_iterator it = solved.begin(); it != solved.end(); it++) {
const roads_t &roads = full_map[it->first];
for(roads_t::const_iterator it2 = roads.begin(); it2 != roads.end(); it2++) {
if(solved.find(it2->second) == solved.end()) {
if(it2->first + it->second < min_path) {
min_path = it2->first + it->second;
min_city = it2->second;
}
break;
}
}
}
if(min_city < 0)
return -1;
solved_count++;
solved[min_city] = min_path;
}
return min_city;
}

int main()
{
int road_data[] = { 0, 1, 120, 0, 2, 180, 1, 3, 40 }; // test data1
cout << kthcity(4, 3, road_data, 3) << endl;
int road_data2[] = { 0, 1, 120, 0, 3, 60, 3, 2, 30}; // test data2
cout << kthcity(4, 3, road_data2, 1) << endl;
return 0;
}

编译运行没问题!
wdx04 2008-09-09
  • 打赏
  • 举报
回复
编译不过换一个编译器(VC7.1+,MingW/GCC3.4+,Intel7+)。

// kthcity.cpp
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <limits>

using namespace std;

int kthcity(int ncity, int nroad, vector<int> road_data, int k)
{
typedef vector<pair<int/*road length*/, int/*to city*/> > roads_t;
typedef map<int/*from city*/, roads_t/*roads to all adjacent cities*/> full_map_t;
typedef map<int/*city*/, int/*distance to city 0*/> solved_t;
full_map_t full_map;
solved_t solved;
int solved_count = 0;
// prepare full map
for(int i = 0; i < nroad; i++) {
int end1 = road_data[i*3];
int end2 = road_data[i*3+1];
int length = road_data[i*3+2];
if(full_map.find(end1) != full_map.end()) {
full_map[end1].push_back(make_pair(length, end2));
}
else {
roads_t roads;
roads.push_back(make_pair(length, end2));
full_map[end1] = roads;
}
if(full_map.find(end2) != full_map.end()) {
full_map[end2].push_back(make_pair(length, end1));
}
else {
roads_t roads;
roads.push_back(make_pair(length, end1));
full_map[end2] = roads;
}
}
for(full_map_t::iterator it = full_map.begin(); it != full_map.end(); it++) {
sort(it->second.begin(), it->second.end());
}
// search
solved[0] = 0;
int min_city = 0;
int min_path = 0;
while(solved_count < k) {
min_path = INT_MAX;
min_city = -1;
for(solved_t::const_iterator it = solved.begin(); it != solved.end(); it++) {
const roads_t &roads = full_map[it->first];
for(roads_t::const_iterator it2 = roads.begin(); it2 != roads.end(); it2++) {
if(solved.find(it2->second) == solved.end()) {
if(it2->first + it->second < min_path) {
min_path = it2->first + it->second;
min_city = it2->second;
}
break;
}
}
}
if(min_city < 0)
return -1;
solved_count++;
solved[min_city] = min_path;
}
return min_city;
}

int main(int argc, char* argv[])
{
int ncity, nroad, k;
vector<int> road_data;
cin >> ncity;
while(ncity != 0) {
cin >> nroad;
for(int i = 0; i < nroad * 3; i++) {
int road_param;
cin >> road_param;
road_data.push_back(road_param);
}
cin >> k;
cout << kthcity(ncity, nroad, road_data, k) << endl;
road_data.clear();
cin >> ncity;
}
return 0;
}

把a5.in和生成的kthcity.exe放到一个目录下运行程序:kthcity <a5.in >a5.out
night_blue 2008-09-09
  • 打赏
  • 举报
回复
Dijkstra 算法 求某源点到其他各点的最短路径

楼主要多看书啊,《数据结构》里面说得很详细了
yao6586107 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 hqin6 的回复:]
C/C++ code

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <limits>

using namespace std;

int kthcity(int ncity, int nroad, int roads[], int k)
{
typedef vector <pair <int, int> > roads_t;
typedef map <int, roads_t> full_map_t;
typedef map <int, int> solved_t;
full_map_t full_map;
solved_t sol…
[/Quote]
强!
conan19870811 2008-09-08
  • 打赏
  • 举报
回复
71 F:\fif.cpp expected unqualified-id before '{' token
71 F:\fif.cpp expected `,' or `;' before '{' token
刚才的错误下一行的大括号显示错误~错误代码如上~那一行只有一个大括号~我不明白怎么错了~高手受累再给看看~只剩这一个错误了~各位的大恩大德我真的难以回报~后天程序就要上交了~对我很重要~我会在加分的~高手哥哥~就靠您了~无尽的感激………………
bxx5203344 2008-09-08
  • 打赏
  • 举报
回复
高手阿
那些英文就高的我云里雾里
K行天下 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 conan19870811 的回复:]
int _tmain(int argc, _TCHAR* argv[]); 这句有个错误~说的是:70 F:\fif.cpp `_TCHAR' has not been declared 大哥~这个怎么回事~~你的确很强啊~
[/Quote]
你的改为:
int main(int argc, char* argv[]);
sunhuanwen 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hqin6 的回复:]
http://download.csdn.net/source/205253
[/Quote]


我看这个就不错,用这个吧
conan19870811 2008-09-08
  • 打赏
  • 举报
回复
int _tmain(int argc, _TCHAR* argv[]); 这句有个错误~说的是:70 F:\fif.cpp `_TCHAR' has not been declared 大哥~这个怎么回事~~你的确很强啊~
yueyucanyang 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wdx04 的回复:]
随手做了一个,不知道对不对(不包括读文件的代码)
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <limits>

using namespace std;

int kthcity(int ncity, int nroad, int roads[], int k)
{
typedef vector <pair <int, int> > roads_t;
typedef map <int, roads_t> full_map_t;
typedef map <int, int> solved_t;
fu…
[/Quote]

厉害!
wdx04 2008-09-08
  • 打赏
  • 举报
回复
改正一个Bug,路是双向连通的

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <limits>

using namespace std;

int kthcity(int ncity, int nroad, int roads[], int k)
{
typedef vector<pair<int, int> > roads_t;
typedef map<int, roads_t> full_map_t;
typedef map<int, int> solved_t;
full_map_t full_map;
solved_t solved;
int solved_count = 0;
// prepare map
for(int i = 0; i < nroad; i++) {
int end1 = roads[i*3];
int end2 = roads[i*3+1];
int length = roads[i*3+2];
if(full_map.count(end1) > 0) {
full_map[end1].push_back(make_pair(length, end2));
}
else {
roads_t roads;
roads.push_back(make_pair(length, end2));
full_map[end1] = roads;
}
if(full_map.count(end2) > 0) {
full_map[end2].push_back(make_pair(length, end1));
}
else {
roads_t roads;
roads.push_back(make_pair(length, end1));
full_map[end2] = roads;
}
}
for(full_map_t::iterator it = full_map.begin(); it != full_map.end(); it++) {
sort(it->second.begin(), it->second.end());
}
// search
solved[0] = 0;
int min_city = 0;
int min_path = 0;
while(solved_count < k) {
min_path = INT_MAX;
min_city = -1;
for(solved_t::const_iterator it = solved.begin(); it != solved.end(); it++) {
const roads_t &roads = full_map[it->first];
for(roads_t::const_iterator it2 = roads.begin(); it2 != roads.end(); it2++) {
if(solved.find(it2->second) == solved.end()) {
if(it2->first + it->second < min_path) {
min_path = it2->first + it->second;
min_city = it2->second;
}
break;
}
}
}
if(min_city < 0)
return -1;
solved_count++;
solved[min_city] = min_path;
}
return min_city;
}

int _tmain(int argc, _TCHAR* argv[])
{
int road_data[] = { 0, 1, 120, 0, 2, 180, 1, 3, 40 }; // test data1
cout << kthcity(4, 3, road_data, 3) << endl;
int road_data2[] = { 0, 1, 120, 0, 3, 60, 3, 2, 30}; // test data2
cout << kthcity(4, 3, road_data2, 1) << endl;
return 0;
}

帅得不敢出门 2008-09-08
  • 打赏
  • 举报
回复
我不是高手.进来jf
wdx04 2008-09-08
  • 打赏
  • 举报
回复
随手做了一个,不知道对不对(不包括读文件的代码)
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <limits>

using namespace std;

int kthcity(int ncity, int nroad, int roads[], int k)
{
typedef vector<pair<int, int> > roads_t;
typedef map<int, roads_t> full_map_t;
typedef map<int, int> solved_t;
full_map_t full_map;
solved_t solved;
int solved_count = 0;
// prepare map
for(int i = 0; i < nroad; i++) {
int end1 = roads[i*3];
int end2 = roads[i*3+1];
int length = roads[i*3+2];
if(full_map.count(end1) > 0) {
full_map[end1].push_back(make_pair(length, end2));
}
else {
roads_t roads;
roads.push_back(make_pair(length, end2));
full_map[end1] = roads;
}
}
for(full_map_t::iterator it = full_map.begin(); it != full_map.end(); it++) {
sort(it->second.begin(), it->second.end());
}
// search
solved[0] = 0;
int min_city = 0;
int min_path = 0;
while(solved_count < k) {
min_path = INT_MAX;
min_city = -1;
for(solved_t::const_iterator it = solved.begin(); it != solved.end(); it++) {
const roads_t &roads = full_map[it->first];
for(roads_t::const_iterator it2 = roads.begin(); it2 != roads.end(); it2++) {
if(solved.find(it2->second) == solved.end()) {
if(it2->first + it->second < min_path) {
min_path = it2->first + it->second;
min_city = it2->second;
}
break;
}
}
}
if(min_city < 0)
return -1;
solved_count++;
solved[min_city] = min_path;
}
return min_city;
}

int _tmain(int argc, _TCHAR* argv[])
{
int road_data[] = { 0, 1, 120, 0, 2, 180, 1, 3, 40 }; // test data1
cout << kthcity(4, 3, road_data, 3) << endl;
int road_data2[] = { 0, 1, 120, 0, 3, 60, 3, 2, 30}; // test data2
cout << kthcity(4, 3, road_data2, 1) << endl;
return 0;
}
hmsuccess 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hqin6 的回复:]
http://download.csdn.net/source/205253
[/Quote]
fengyizi 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hqin6 的回复:]
http://download.csdn.net/source/205253
[/Quote]
jieao111 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hqin6 的回复:]
http://download.csdn.net/source/205253
[/Quote]
太乙 2008-09-08
  • 打赏
  • 举报
回复
http://download.csdn.net/source/205253
太乙 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 conan19870811 的回复:]
跪求高手编一个 ……
[/Quote]

网上有很多现成的例子!
你找一个!
conan19870811 2008-09-08
  • 打赏
  • 举报
回复
跪求高手编一个 ……
太乙 2008-09-08
  • 打赏
  • 举报
回复
just so easy!

help yourself!
加载更多回复(7)

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧