青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

oyjpArt ACM/ICPC算法程序設(shè)計空間

// I am new in programming, welcome to my blog
I am oyjpart(alpc12, 四城)
posts - 224, comments - 694, trackbacks - 0, articles - 6

PKU1734 Sightseeing trip (CEOI99)

Posted on 2007-05-28 23:41 oyjpart 閱讀(2398) 評論(3)  編輯 收藏 引用 所屬分類: ACM/ICPC或其他比賽
很久沒寫結(jié)題報告了
今天做Sightseeing trip 上來貼個
Ural:1004

Sightseeing trip
Time Limit:1000MS  Memory Limit:65536K
Total Submit:317 Accepted:133 Special Judged

Description
There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is necessary to find the shortest route which begins and ends at the same place. Your task is to write a program which finds such a route.

In the town there are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y_1, ..., y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, the road y_k connects crossing points x_k and x_1. All the numbers x_1,...,x_k should be different.The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L(y_1)+L(y_2)+...+L(y_k) where L(y_i) is the length of the road y_i (1<=i<=k). Your program has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible,because there is no sightseeing route in the town.

Input
The first line of input contains two positive integers: the number of crossing points N<=100 and the number of roads M<=10000. Each of the next M lines describes one road. It contains 3 positive integers: the number of its first crossing point, the number of the second one, and the length of the road (a positive integer less than 500).

Output
There is only one line in output. It contains either a string 'No solution.' in case there isn't any sightseeing route, or it contains the numbers of all crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x_1 to x_k from our definition of a sightseeing route), separated by single spaces. If there are multiple sightseeing routes of the minimal length, you can output any one of them.

Sample Input

5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20

 

Sample Output

1 3 5 2

 

點數(shù)是100個 題目意思是找一個最小權(quán)圈 以任意序輸出
從圖論的角度上考慮 應該是任意選一條邊(枚舉) 然后刪除邊 再以一個點為原點求Dijkstra 找出最小權(quán)圈 o(M*N^2)的復雜度 一個更加好的算法是限定枚舉的點為圈內(nèi)序號最大的點 這樣就避免了對一個圈的多次枚舉(參考程序3)
如果直接搜就是任選一個點開始走回到原點則記錄長度 搜的時候必須要先對每個點的邊按照邊權(quán)進行排序 以備后面大量剪枝

程序1

 1//Solution
 2//by oyjpArt
 3//Algorithm:Search
 4#include <vector>
 5#include <iostream>
 6#include <algorithm>
 7using namespace std;
 8
 9const int N = 101;
10struct Node {int x, w; void set(int xx, int ww) {x =xx; w = ww; }};
11vector<Node> adj[N];
12int nv, ne, ans[N], na, S, rec[N];
13bool chk[N];
14int best;
15
16bool operator<(const Node& a, const Node& b) {
17 return a.w < b.w;
18}
19
20void search(int x, int sum, int depth, int father) {
21 int i;
22 if(x == S && chk[x]) {
23  if(sum < best) {
24   best = sum;  na = depth;
25   for(i = 0; i < depth; i++) ans[i] = rec[i];
26  }
27  return;
28 }
29 rec[depth] = x;
30 for(i = 0; i < adj[x].size(); ++i) if(adj[x][i].x != father) if(!chk[adj[x][i].x] || adj[x][i].x == S) {
31  chk[adj[x][i].x] = 1;
32  if(sum + adj[x][i].w < best) search(adj[x][i].x, sum + adj[x][i].w, depth+1, x);
33  chk[adj[x][i].x] = 0;
34 }
35
36
37int main() {
38 scanf("%d %d"&nv, &ne);
39 int i, u, v, w;
40 Node now;
41 for(i = 0; i < ne; i++) {
42  scanf("%d %d %d"&u, &v, &w);
43  --u; --v;
44  now.set(v, w);
45  adj[u].push_back(now);
46  now.x = u;
47  adj[v].push_back(now);
48 }
49 for(i = 0; i < nv; ++i) 
50  sort(adj[i].begin(), adj[i].end());
51 
52 best = 123456789;
53 for(i = 0; i < nv; ++i) {
54  memset(chk, 0, nv * sizeof(bool));
55  S = i;
56  search(i, 00-1);
57 }
58
59 if(best == 123456789) { printf("No solution.\n"); return 0; }
60 printf("%d", ans[0]+1);
61 for(i = 1; i < na; ++i) printf(" %d", ans[i]+1); putchar('\n');
62
63 return 0;
64}
65
66



程序2

 1//Solution
 2//by oyjpArt
 3//Algorithm : Enumerate + Dijkstra
 4#include <stdio.h>
 5#include <string.h>
 6
 7const int N = 101, M = 20001, MAXINT = 2000000000;
 8int ne, nv;
 9struct E {
10 int x, w; E* next;
11 void set(int xx, int ww, E* nn) {x = xx; w = ww; next = nn;}
12}e[M], * head[N];
13int best, dist[N], q[N], ans[N], pre[N], na;
14bool chk[N];
15
16void Dijk(int st, int endint ow) {
17 memset(chk, 0, sizeof(chk));
18 memset(dist, -1, sizeof(dist));
19 int qe = 1, qs = 0, i;
20 E * p;
21 for(i = 0; i < nv; ++i) if(i != st) {
22  for(p = head[st]; p != NULL; p = p->next) {
23   if(p->== i && p->> 0 && (dist[i] == -1 || dist[i] > p->w ) ) 
24    dist[i] = p->w;
25  }
26  if(dist[i] == -1) dist[i] = MAXINT;
27 }
28 q[0= st;
29 dist[st] = 0;
30 chk[st] = 1;
31 for(i = 0; i < nv; ++i) pre[i] = st;
32 pre[st] = -1;
33 while(qs < qe) {
34  int cur = q[qs++];
35  chk[cur] = 1;
36  if(ow + dist[cur] >= best) return;
37  if(cur == end) {
38   if(dist[end+ ow < best) {
39    na = 0;
40    for(i = cur; i != -1; i = pre[i]) ans[na++= i;
41    best = dist[end+ ow;
42   }
43   return;
44  }
45  int _min = MAXINT, mini = -1;
46  for(i = 0; i < nv; i++if(!chk[i]) {
47   if(dist[i] < _min) {
48    _min = dist[i];
49    mini = i;
50   }
51  }
52  if(mini == -1) return;
53  q[qe++= mini;
54  for(i = 0; i < nv; ++i) if(!chk[i]) {
55   for(p = head[mini]; p != NULL; p = p->nextif(p->== i)  break;
56   if(p == NULL) continue;
57   if(p->> 0 && p->+ dist[mini] < dist[i]) {
58    dist[i] = p->+ dist[mini];
59    pre[i] = mini;
60   }
61  }
62 }
63}
64
65int main() {
66 scanf("%d %d"&nv, &ne);
67 memset(head, NULL, nv * sizeof(E*));
68 int i, u, v, w;
69 for(i = 0; i < ne; ++i) {
70  scanf("%d %d %d"&u, &v, &w);
71  --u; --v;
72  e[2*i].set(u, w, head[v]);
73  head[v] = &e[2*i];
74  e[2*i+1].set(v, w, head[u]);
75  head[u] = &e[2*i+1];
76 }
77 E * p, * q;
78 best = MAXINT;
79 for(i = 0; i < nv; ++i) {
80  for(p = head[i]; p != NULL; p = p->next) {
81   int w = p->w;
82   int j = p->x;
83   for(q = head[i]; q != NULL; q = q->nextif(q->== j) q->= -q->w;
84   for(q = head[j]; q != NULL; q = q->nextif(q->== i) q->= -q->w;
85   Dijk(i, j, w);
86   for(q = head[i]; q != NULL; q = q->nextif(q->== j) q->= -q->w;
87   for(q = head[j]; q != NULL; q = q->nextif(q->== i) q->= -q->w;
88  }
89 }
90 if(best == MAXINT) printf("No solution.\n");
91 else {
92  printf("%d", ans[0+ 1);
93  for(i = 1; i < na; ++i) printf(" %d", ans[i] + 1); putchar('\n');
94 }
95 return 0;
96}
97//唉 不用vector代碼量增大好多。。暈倒
98

程序3:
經(jīng)wywcgs大牛提醒 改寫成了Floyd程序 時間銳減
 1#include <stdio.h>
 2#include <string.h>
 3
 4const int N = 101;
 5const int MAXINT = 123456789;
 6int ne, nv;
 7int adj[N][N];
 8int pre[N][N];
 9int conn[N][N];
10int na, ans[N];
11int best;
12
13void floyd() {
14    int i, j, k, tmp, p;
15    for(k = 0; k < nv; ++k) {
16        for(i = 0; i < k; ++i) {
17            for(j = 0; j < k; ++j) if(conn[i][k] && conn[k][j] && j != i) {
18                if( (tmp = adj[i][j] + conn[k][i] + conn[j][k]) < best) {
19                    best = tmp;
20                    na = 1; ans[0= k; p = i;
21                    while(p != -1) {
22                        ans[na++= p;
23                        p = pre[p][j];
24                    }
25                }
26            } 
27        }
28        for(i = 0; i < nv; ++i) 
29            for(j = 0; j < nv; ++j) {
30                if(adj[i][j] > adj[i][k] + adj[k][j]) {
31                    adj[i][j] = adj[i][k] + adj[k][j];
32                    pre[i][j] = pre[i][k];
33                }
34            }
35    }
36}
37
38int main() {
39    int i, j, u, v, w;
40    memset(pre, -1, sizeof(pre));
41    scanf("%d %d"&nv, &ne);
42    for(i = 0; i < nv; ++i) {
43        for(j = i+1; j < nv; ++j) 
44            adj[i][j] = adj[j][i] = MAXINT;
45        adj[i][i] = 0;
46    }
47    for(i = 0; i < ne; ++i) {
48        scanf("%d %d %d"&u, &v, &w);
49        --u; --v;
50        if(w < adj[u][v])     
51            conn[u][v] = conn[v][u] = adj[u][v] = adj[v][u] = w;
52        pre[u][v] = v, pre[v][u] = u;
53    }
54    best = MAXINT;
55    floyd();
56    if(best == MAXINT) printf("No solution.\n");
57    else {
58        for(i = 0; i < na; ++i) {
59            printf("%d", ans[i] + 1);
60            if(i != na-1) putchar(' ');
61            else putchar('\n');
62        }
63    }
64
65    return 0;
66}
67

Feedback

# re: PKU1734 Sightseeing trip (CEOI99)  回復  更多評論   

2007-05-30 22:08 by wywcgs
呃……這道題有O(V^3)做法……一次floyd或者V次dijkstra……
你可以再想想,總之不是很難……

# re: PKU1734 Sightseeing trip (CEOI99)  回復  更多評論   

2007-07-28 17:27 by dingyihamigua
大牛人啊!!
很經(jīng)典的東西!!
謝謝了哈!
辛苦了!!

# re: PKU1734 Sightseeing trip (CEOI99)  回復  更多評論   

2011-11-10 15:58 by wuyiqi
我感覺如果用搜索的話好像排序與不排序是一樣的吧,因為一個點是等可能的搜向鄰邊的,排序改變不了什么,就算先搜到最短的鄰邊,隨后還是有可能越走越遠
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            蜜臀av性久久久久蜜臀aⅴ四虎 | 国内自拍一区| 欧美激情一区二区三级高清视频| 亚洲欧美激情视频| 亚洲另类一区二区| 免费成人在线视频网站| 午夜精品影院在线观看| 99国产精品私拍| 在线观看日韩av| 国产女主播视频一区二区| 欧美成人一区二免费视频软件| 欧美在线亚洲综合一区| 亚洲淫性视频| avtt综合网| 亚洲免费不卡| 亚洲精品久久在线| 亚洲电影av在线| 亚洲第一精品夜夜躁人人爽| 久久深夜福利| 久久久久一区二区| 久久精品二区亚洲w码| 午夜激情久久久| 午夜国产精品视频免费体验区| 亚洲午夜av在线| 一区二区三区黄色| 亚洲小视频在线| 亚洲午夜激情网站| 一区二区三区四区五区在线| 99天天综合性| 一区二区三区导航| 亚洲视频在线二区| 亚洲午夜视频在线| 亚洲一区二区三区中文字幕在线| 一区二区三区四区国产精品| 中文在线资源观看视频网站免费不卡| 亚洲精品国产精品乱码不99 | 亚洲毛片播放| 日韩视频―中文字幕| aa级大片欧美三级| 亚洲一区二区在线视频| 亚洲欧美视频在线观看| 午夜影视日本亚洲欧洲精品| 欧美一级成年大片在线观看| 性色av香蕉一区二区| 性亚洲最疯狂xxxx高清| 久久精品国产99国产精品澳门| 欧美影院成年免费版| 久久精品一区二区国产| 狂野欧美激情性xxxx欧美| 男人的天堂成人在线| 欧美精品一区二区三区在线看午夜 | 欧美日本久久| 国产精品久久久久影院色老大| 国产精品美女一区二区在线观看 | 一区二区三区福利| 亚洲专区国产精品| 久久久久成人精品| 欧美成人精品影院| 欧美日韩中国免费专区在线看| 国产精品久久久亚洲一区| 国产欧美精品在线| 亚洲丰满少妇videoshd| 一区二区欧美精品| 久久国产精品一区二区三区四区| 老司机午夜精品视频在线观看| 亚洲国产日韩欧美在线99 | 在线午夜精品自拍| 欧美一级淫片播放口| 嫩草影视亚洲| 亚洲另类黄色| 久久精品国产77777蜜臀| 欧美国产日韩精品| 国产欧美日韩一区| 亚洲区中文字幕| 欧美一级视频精品观看| 奶水喷射视频一区| 在线综合亚洲欧美在线视频| 久久gogo国模啪啪人体图| 欧美激情一区二区三区在线视频观看 | 亚洲黄色精品| 欧美一级专区| 欧美人与禽猛交乱配| 国产欧美短视频| 亚洲精品国产精品国自产观看 | 亚洲激情av在线| 午夜日本精品| 欧美精品一区二区高清在线观看| 国产精品裸体一区二区三区| 亚洲丰满在线| 欧美在线综合视频| 亚洲精品久久久久久久久久久| 久久爱www久久做| 欧美日韩国产在线一区| 激情自拍一区| 性欧美videos另类喷潮| 亚洲精选视频在线| 久久久亚洲成人| 国产欧美精品日韩精品| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 亚洲视频一二| 女同一区二区| 久久福利电影| 国产欧美视频一区二区| 一区二区三区四区五区视频 | 免费一级欧美在线大片| 国产亚洲成精品久久| 亚洲午夜小视频| 亚洲国产欧美一区| 久久在线免费视频| 狠狠爱综合网| 翔田千里一区二区| 中文av一区二区| 欧美日韩亚洲91| 日韩一二三区视频| 亚洲成色最大综合在线| 久久久精彩视频| 国产亚洲人成网站在线观看| 香蕉av777xxx色综合一区| 99ri日韩精品视频| 欧美精品在线一区| 亚洲美女av网站| 亚洲国产人成综合网站| 欧美国产日韩a欧美在线观看| 亚洲国产老妈| 欧美电影免费观看高清完整版| 久久精品盗摄| 精品999久久久| 久色婷婷小香蕉久久| 久久成人在线| 伊人久久大香线蕉综合热线| 美乳少妇欧美精品| 久久综合免费视频影院| 亚洲国产欧美日韩另类综合| 欧美高潮视频| 欧美成人精品在线| 日韩视频二区| 一本色道久久88综合亚洲精品ⅰ | 亚洲小视频在线观看| 在线一区二区三区四区五区| 欧美日韩一区三区| 午夜精品久久| 欧美一级播放| 亚洲第一区色| 亚洲韩日在线| 欧美三级电影一区| 欧美在线影院| 玖玖视频精品| 亚洲视频一二| 午夜精品理论片| 狠狠色丁香久久婷婷综合_中| 久久资源在线| 欧美激情一区二区三区高清视频| 亚洲天堂黄色| 亚洲欧美一区二区三区久久| 国语自产精品视频在线看8查询8| 另类亚洲自拍| 欧美精品系列| 欧美一区三区三区高中清蜜桃| 久久精品国产亚洲一区二区| 亚洲国产精品成人一区二区 | 亚洲三级网站| 亚洲一区二区三区色| 国产综合色在线视频区| 欧美国产日本高清在线| 欧美日韩视频第一区| 久久精品72免费观看| 免费日韩一区二区| 亚洲欧美激情视频在线观看一区二区三区| 欧美一级二级三级蜜桃| 亚洲精品久久嫩草网站秘色| 中日韩男男gay无套| 一区在线电影| 正在播放欧美视频| **网站欧美大片在线观看| 9色精品在线| 一区在线播放视频| 在线综合欧美| 91久久综合| 欧美一区二区黄| 一区二区三区福利| 久久视频国产精品免费视频在线| 亚洲视频欧美视频| 久久三级视频| 久久国产福利国产秒拍| 欧美国产先锋| 久久午夜激情| 国产精品久久999| 亚洲福利视频二区| 国产综合av| 9久草视频在线视频精品| 亚洲国产专区| 欧美一站二站| 亚洲欧美日韩精品在线| 欧美精品一区二区蜜臀亚洲| 久久久久久久久伊人| 欧美午夜无遮挡| 亚洲高清在线播放| 狠狠入ady亚洲精品经典电影| 亚洲亚洲精品三区日韩精品在线视频 | 亚洲美洲欧洲综合国产一区|