-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path图论.txt
2107 lines (1972 loc) · 51.4 KB
/
图论.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 哈夫曼树 求 wpl
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
// 代表小顶堆的优先队列
priority_queue<long long, vector<long long>, greater<long long> > q;
int main(){
int n;
long long temp, x, y, ans= 0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lld",&temp);
q.push(temp); //将初始重量压入优先队列
}
while(q.size() > 1){ // 只要优先队列中至少有两个元素
x = q.top();
q.pop();
y = q.top();
q.pop();
q.push(x+ y); // 取出堆顶的两个元素,求和后压入优先队列
ans += x+y; // 累计求和的结果
}
printf("%lld\n",ans); //
return 0;
}
图论/*
图论 邻接矩阵和邻接表 版本 代码
#include<cstdio>
using namespace std;
const int MAXV = 1000; //设置最大顶点个数
const int INF = 100000000; //设置inf 为一个很大的数
int n,G[MAXV][MAXV]; //n为顶点个数 maxv 为最大顶点数
bool vis[MAXV] = {false}; //如果顶点 i 已被访问,则 vis[i]= true 初值为false
void DFS(int u,int depth){ //u为当前访问的顶点符号,depth为深度
vis[u] = true; //设置u已被访问
//如果需要对u 进行一些操作,可以在这里进行
//下面对所有从u出发能到达的分支顶点进行枚举
for(int v= 0; v<n;v++){ //对每个顶点进行遍历
if( vis[v] == false && G[u][v] !=INF){ //如果v未被访问且u可到达v
DFS(v,depth +1); //访问v depth +1
}
}
}
void DFSTrave(){
for(int u=0;u<n;u++){
if(vis[u] = false){
DFS(u, 1);
}
}
} */
// 邻接表版本
#include<cstdio>
#include<vector>
using namespace std;
const int MAXV=1000;
vector<int> Adj[MAXV];
int n;
bool vis[MAXV] = {false};
void DFS(int u,int depth){
vis[u] = true;
for(int i=0;i<Adj[u].size();i++){
int v = Adj[u][i];
if(vis[v] = false){
DFS(v , depth +1);
}
}
}
void DFSTrave(){
for(int u=0;u<n;u++){
if( vis[u] == false){
DFS(u,1);
}
}
}
/*
图论 head of Gang PAT A1034
*/
#include<string>
#include<map>
#include<iostream>
using namespace std;
const int maxn = 2010; //总人数
const int INF = 10000000; //无穷大
map<int, string> intToString; //编号 姓名
map<string,int> stringToInt; //姓名->编号
map<string, int> Gang; // head-> 人数
int G[maxn][maxn] = {0}, weight[maxn] ={0};//邻接矩阵G,点权 weight
int n,k,numPerson = 0;
bool vis[maxn] = {false};
// DFS函数访问单个连通块, nowVisit 为当前访问的编号
// head 为头目 ,numMember 为成员编号 totalValue 为连通块的总边权
void DFS(int nowVisit, int& head,int& numMember, int& totalValue){
numMember++; //成员人数 加1
vis[nowVisit] = true; //标记nowVist 已访问
if(weight[nowVisit] > weight[head]){
head = nowVisit; //当前访问结点的点权大于头目的点权,则更新头目
}
for(int i=0;i<numPerson;i++){ //枚举所有人
if(G[nowVisit][i] > 0){ //如果从nowVist 能到达 i
totalValue += G[nowVisit][i]; //连通块的总边权增加该边权
G[nowVisit][i] = G[i][nowVisit] = 0; //删除这条边,防止回头
if( vis[i] == false){ // 如果i未被访问,则递归访问i
DFS(i,head,numMember,totalValue);
}
}
}
}
void DFSTrave(){
for(int i=0;i< numPerson; i++){ //枚举 所有人
if(vis[i] == false){ //如果i未被访问
int head = i,numMember = 0,totalValue = 0; //头目,成员数,总边权
DFS(i, head, numMember, totalValue); //遍历i所在的连通块
if(numMember > 2 && totalValue > k){ //成员数大于 2且总边权 大于k
// head 人数为numMember
Gang[intToString[head]] = numMember;
}
}
}
}
//change 函数 返回 姓名 str 对应的编号
int change(string str){
if(stringToInt.find(str) != stringToInt.end() ){
return stringToInt[str]; //返回编号
}else{
stringToInt[str] = numPerson; //str编号为numMember
intToString[numPerson] = str; //numPerson 对应 str
return numPerson++; //总人数 +1
}
}
int main(){
int w;//边的权值
string str1,str2; //编号
cin >> n>> k;
for(int i=0;i<n;i++){
cin >> str1 >> str2 >> w; //输入边的两个端点 和点权
int id1 = change(str1); //将str1转换成id1
int id2 = change(str2);
weight[id1] += w; //id1 的点权 增加w
weight[id2] += w; //id2 的点权 增加 w
G[id1][id2] += w;//边id1-> id2 边权增加 w
G[id2][id1] += w;
}
DFSTrave();
cout <<Gang.size()<<endl;
map<string, int>::iterator it;
for(it = Gang.begin();it !=Gang.end();it++){
cout<< it->first << " " <<it->second <<endl;
}
return 0;
}
/////bfs 邻接矩阵版本
int n,G[MAXV][MAXV];
bool inq[MAXV] ={false};
void BFS(int u){
queue<int> q;
q.push(u);
inq[u] = true;
while( !q.empty() ){
int u = q.front();
q.pop();
for(int v=0;v<n;v++){
if( inq[v] == false &&G[u][v] !=INF){
q.push(v);
inq[v] = true;
}
}
}
}
void BFSTrave(){
for(int u=0;u<n;u++){
if( inq[u] == false){
BFS(q);
}
}
}
//临界版本
vector<int> Adj[MAXV];
int n;
bool inq[MAXV] = {false};
void BFS(int u){
queue<int> q;
q.push(u);
inq[u] = true;
while( !q.empty() ){
int u = q.front();
q.pop();
for(int i=0;i<Adj[u].size();i++){
int v = Adj[u][i];
if(inq[v] == false){
q.push(v);
inq[v] = true;
}
}
}
}
void BFSTrave(){
for(int u=0;u<n;u++){
if(inq[u] == false){
BFS(q);
}
}
}
///存放顶点层号和编号
struct node{
int v;
int layer;
};
void BFS(int s){
queue<Node> q;
Node start;
start.v = s;
start.layer = 0;
q.push(start);
inq[start.v] = true;
while( !q.empty() ){
Node topNode = q.front();
q.pop();
int u=topNode.v;
for(int i=0;i<Adj[u].size();i++){
Node next = Adj[u][i];
next.layer = topNodelayer +1;
if(inq[next.v] == false){
q.push(next);
inq[next.v] = true;
}
}
}
}
//pat A1076
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int MAXV = 1010;
struct node{
int id; //结点编号
int layer; //结点层号
};
vector<node> Adj[MAXV]; //邻接表
bool inq[MAXV] = {false}; //顶点是否已被加入过队列
int BFS(int s,int L){ //start 为起点,L为层数上仙
int numForward = 0; //转发数
queue<node> q;//BFS队列
node start;//定义起始结点
start.id = s; //起始结点编号
start.layer = 0; //起始结点层号为 0
q.push(start); //将起始结点压入队列
inq[start.id] = true; //起始结点编号设为 已被加入过队列
while( !q.empty() ){
node topNode = q.front(); //取出队首结点
q.pop(); //队首结点 出队
int u = topNode.id; //队首结点的编号
for(int i=0; i< Adj[u].size() ; i++){
node next = Adj[u][i];
next.layer = topNode.layer + 1;
if( inq[next.id] == false && next.layer <= L){
//如果next 编号 未被加入过队列,且next 的层次不超过上限L
q.push(next);
inq[next.id] = true;
numForward++;
}
}
}
return numForward; // 返回转发数
}
int main(){
node user;
int n,L,numFollow, idFollow;
scanf("%d %d",&n,&L);
for(int i=1;i<=n;i++){
user.id = i;
scanf("%d",&numFollow);
for( int j=0; j<numFollow;j++){
scanf("%d",&idFollow);
Adj[idFollow].push_back(user);
}
}
int numQuery, s;
scanf("%d",&numQuery);
for(int i=0;i<numQuery;i++){
memset(inq,false , sizeof(inq)) ;
scanf("%d", &s);
int numForward = BFS(s, L);
printf("%d\n",numForward);
}
return 0;
}
codeup A连通图
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
vector<int>map[1000010];
bool vis[1000010] ={false} ;
bool HASH[1000010]={false};
void bfs(int u)
{
queue<int>q;
q.push(u);
vis[u] = true;
while (!q.empty())
{
int top = q.front();
q.pop();
for (int i=0;i<map[top].size();i++)
{
int v = map[top][i];
if (vis[v]==false)
{
q.push(v);
vis[v] = true;
}
}
}
}
int BFS(int n)
{
int ans=0;
for (int u=0;u<=n;u++)
{
if (vis[u]==false && HASH[u]==true)
{
bfs(u);
ans++;
}
}
return ans;
}
int main()
{
int n=0,m,j,k,i,T,a,c,b,d;
while(cin>>a>>b)
{
n = max(n,max(a,b));
map[a].push_back(b);
map[b].push_back(a);
HASH[a] = HASH[b] = true;
}
cout<<BFS(n)<<endl;
return 0;
}
用dfs 解决
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 10000024;
vector<int> G[maxn];
int vis[maxn];
void DFS(int u){
vis[u] =1;
for(int i=0;i<G[u].size();i++){
int v= G[u][i];
if( vis[v] == 0){
DFS(v);
}
}
}
int main(){
int n = -1;
int u,v;
while( scanf("%d %d",&u,&v) !=EOF){
memset(vis, 0 ,sizeof(vis));
if( u != v){
G[u].push_back(v);
G[v].push_back(u);
}else{
G[u].push_back(v);
}
n = max(n,u);
n = max(n,v);
}
int ans = 0;
for(int i=1;i<=n;i++){
if(vis[i] == 0 &&G[i].size() >0){
DFS(i);ans++;
}
}
printf("%d",ans);
return 0;
}
//codeup b 连通图 http://codeup.cn/problem.php?cid=100000620&pid=1
//并查集
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int nmax=1010;
int father[nmax];
int isRoot[nmax];
int findFather(int u){
if(u==father[u]) return u;
else{
int f=findFather(father[u]);
father[u]=f;
return f;
}
}
void Union(int u,int v){
int fu=findFather(u);
int fv=findFather(v);
if(fu!=fv){
father[fu]=fv;
}
}
void init(int n){
for(int i=1;i<=n;i++){
father[i]=i;
isRoot[i]=0;
}
}
int main() {
int n,m;//点数,边数
int u,v;
while( scanf("%d %d",&n,&m)!=EOF && n+m){
init(n);
for(int i=0;i<m;i++){
scanf("%d %d",&u,&v);
Union(u,v);
}
int ans=0;
for(int i=1;i<=n;i++){
if(father[i]==i){
ans++;
}
}
printf(ans==1 ? "YES\n" :"NO\n");
}
return 0;
}
//图的DFS遍历 计算连通块数
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int nmax=1010;
vector<int> G[nmax];
int vis[nmax];//1已被访问 0未被访问
//遍历连通块
void DFS(int u){
vis[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(vis[v]==0){//如果该节点未被访问,则深度遍历
DFS(v);
}
}
G[u].clear();
}
int main(int argc, char** argv) {
int n;//点数
int m;//边数
while(cin>>n>>m){
if(n==0){
break;
}
memset(vis,0,sizeof(vis));
int u,v;
for(int i=0;i<m;i++){
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
int blk=0;
//遍历整个图G
for(int i=1;i<=n;i++){
if(vis[i]==0){
DFS(i);//访问i所在的连通块
blk++;
}
}
//cout<<blk<<endl;
if(blk==1){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}
#include<cstdio>
using namespace std;
const int MAXV = 1000;
const int INF = 100000000;
int n,G[MAXV][MAXV];
int d[MAXV];
bool vis[MAXV] = {false};
//邻接矩阵
void Dijkstra(int s){
fill(d, d+MAXV,INF); //慎用memset
d[s] = 0;
for(int i=0;i<n;i++){
int u = -1,MIN = INF;
for(int j=0;j<n;j++){
if(vis[j] == false && d[j] <MIN){
u = j;
MIN = d[j];
}
}
//找不到小于INF 的d[u],说明剩下的顶点和起点s不连通
if( u == -1) return ;
vis[u] = true;
for(int v=0;v<n;v++){
//如果v未访问&& u 能到达v && 以u为中介点可以是d[v]更优
if(vis[v] == false && G[U][V] != INF && d[u] + G[u][v] <d[v]){
d[v] = d[u] + G[u][v];//优化d[v]
}
}
}
}
//邻接表法
#include<cstdio>
using namespace std;
const int MAXV =1000;
const int INF = 1000000;
struct Node{
int v,dis;
};
vector<Node> Adj[MAXV];
int n;
int d[MAXV];
bool vis[MAXV] = {false};
void Dijkstra(int s){
fill(d,d+MAXV,INF);
d[s] = 0;
for(int i=0;i<n;i++){
int u = -1, MIN = INF;
for(int j=0;j<n;j++){
if( vis[j] == false && d[j] <MIN){
u = j;
MIN = d[j];
}
}
//找不到比inf小的d[u ],说明剩下的顶点和起点s不连通
if(u == -1) return ;
vis[u] = true;
//只是下面这个 for 和 邻接矩阵写法不同
for(int j=0;j<Adj[u].size();j++){
int v = Adj[u][j].v;
if( vis[v] == false && d[u] +Adj[u][j].dis <d[v]){
//如果v未访问&& 以u为中介点可以更优 更新
d[v] = d[u] + Adj[u][v].dis;
}
}
}
}
最短路径 dijkstra
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXV = 1000;
const int INF = 10000000;
int n,m,s, G[MAXV][MAXV]; //n为顶点数,m为边数 s为起点
int d[MAXV]; //起点到达各点的最短路径长度
bool vis[MAXV] = {false};
void Dijkstra(int s){
fill(d , d+MAXV,INF);
d[s] = 0;
for(int i=0;i<n;i++){
int u = -1, MIN = INF;
for(int j=0;j<n;j++){
if( vis[j] == false && d[j] < MIN){
u = j;
MIN = d[j];
}
}
if( u == -1) return ;
vis[u] = true;
for(int v=0;v<n;v++){
if( vis[v] == false && G[u][v] !=INF && d[u]+G[u][v] <d[v]){
d[v] = d[u] +G[u][v];
}
}
}
}
int main(){
int u,v,w;
scanf("%d %d %d",&n,&m,&s); //顶点个数 边数 起点编号
fill(G[0], G[0] +MAXV *MAXV , INF);
for(int i=0;i<m;i++){
scanf("%d %d %d",&u,&v,&w); //顶点个数 边数 起点编号
G[u][v] = w;
}
Dijkstra(s);
for(int i=0;i<n;i++){
printf(i ? "%d":" %d",d[i]);
}
return 0;
}
//pat 1A1003
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXV = 510;
const int INF = 10000000000;
// n 为定点数,m为边数 st和ed 分别为起点和终点
//g为邻接矩阵,weight 为点权
//d[]记录最短距离w[]记录最大点权之和,num[]记录最短路径条数
int n,m,st,ed,G[MAXV][MAXV],weight[MAXV];
int d[MAXV], w[MAXV],num[MAXV];
bool vis[MAXV] = {false};
void Dijkstra(int s){
fill(d, d+MAXV,INF);
memset(num, 0,sizeof(num));
memset(w,0,sizeof(w));
d[s] =0;
w[s] = weight[s];
num[s] = 1;
for(int i=0;i<n;i++){
int u = -1,MIN = INF;
for(int j=0;j<n;j++){
if(vis[j] == false && d[j] < MIN){
u = j;
MIN = d[j];
}
}
//找不到小于INF 的d[u] 说明剩下的顶点和起点s不连通
if( u==-1) return ;
vis[u] = true;
for(int v=0;v<n;v++){
//如果v未访问 && u能到达v && 以u为中介点可以使d[v] 更优
if(vis[v] == false && G[u][v] !=INF){
if(d[u] + G[u][v] <d[v]){ //以u为中介点能令d[v]变小
d[v] = d[u] +G[u][v];
w[v] = w[u] +weight[v];
num[v] = num[u];
}else if(d[u] +G[u][v] == d[v]){ //找到一条相同长度的路径
if(w[u] + weight[v] > w[v]){ //以u 为中介点时点权之和更大
w[v] = w[u] +weight[v]; // w[v] 继承w[u]
}
//最短路径条数与点权无关,必须写外面
num[v] +=num[u];
}
}
}
}
}
int main(){
scanf("%d %d %d %d",&n,&m,&st,&ed);
for(int i=0;i<n;i++){
scanf("%d",&weight[i]); //读入点权
}
int u,v;
fill( G[0],G[0] + MAXV*MAXV,INF); //初始化
for(int i=0;i<m;i++){
scanf("%d %d",&u,&v);
scanf("%d",&G[u][v]); //读入边权
G[v][u] = G[u][v];
}
Dijkstra(st);
printf("%d %d\n",num[ed],w[ed]); //最短路径条数,最短路径最大点权值
return 0;
}
//pat A1030
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXV = 510;
const int INF = 100000000;
//n为顶点数,m为边数 st和 ed 分别为起点终点
//G为距离矩阵,cost 为花费
// d[]记录最短距离,c[]记录最小花费
int n,m,st,ed,G[MAXV][MAXV],cost[MAXV][MAXV];
int d[MAXV], c[MAXV],pre[MAXV];
bool vis[MAXV] = {false};
void Dijkstra(int s){
fill(d,d+MAXV,INF);
fill(c,c+MAXV,INF);
for(int i=0;i<n;i++) pre[i] = i;
d[s] = 0;
c[s] = 0;
for(int i=0;i<n;i++){
int u = -1,MIN = INF;
for(int j=0;j<n;j++){
if(vis[j] == false && d[j] <MIN){
u = j;
MIN = d[j];
}
}
//找不到小于INF的d[u],说明剩下的顶点和起点不连通
if( u ==-1) return ;
vis[u] = true;
for(int v= 0;v<n;v++){
if(vis[v] == false && G[u][v] !=INF){
if(d[u] + G[u][v] <d[v]){
d[v] = d[u] +G[u][v];
c[v] = c[u] +cost[u][v];
pre[v] = u;
} else if(d[u] +G[u][v] == d[v]){
if(c[u] +cost[u][v] <c[v]){
c[v] = c[u] +cost[u][v];
pre[v] = u;
}
}
}
}
}
}
void DFS(int v){
if(v == st){
printf("%d ",v);
return;
}
DFS(pre[v]);
printf("%d ",v);
}
int main(){
scanf("%d %d %d %d",&n,&m,&st,&ed);
int u,v;
fill(G[0],G[0] + MAXV*MAXV,INF);
for(int i=0;i<m;i++){
scanf("%d %d",&u,&v);
scanf("%d %d",&G[u][v],&cost[u][v]);
G[v][u] = G[u][v];
cost[v][u] = cost[u][v];
}
Dijkstra(st);
DFS(ed);
printf("%d %d\n",d[ed],c[ed]);
return 0;
}
dj +dfs pat a1030
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXV = 510;
const int INF = 10000000;
int n,m,st,ed,G[MAXV][MAXV],cost[MAXV][MAXV];
int d[MAXV],minCost =INF;
bool vis[MAXV] = {false};
vector<int> pre[MAXV];
vector<int> tempPath , path;
void Dijkstra(int s){
fill(d,d+MAXV,INF);
d[s] = 0;
for(int i=0;i<n;i++){
int u = -1,MIN = INF;
for(int j=0;j<n;j++){
if(vis[j] == false && d[j] <MIN){
u = j;
MIN = d[j];
}
}
if( u ==-1) return ;
vis[u] = true;
for(int v=0;v<n;v++){
if(vis[v] == false && G[u][v] !=INF){
if(d[u] +G[u][v] <d[v]){
d[v] = d[u] +G[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(d[u] + G[u][v] == d[v]){
pre[v].push_back(u);
}
}
}
}
}
void DFS(int v){
if( v == st){
tempPath.push_back(v);
int tempCost = 0;
for(int i= tempPath.size() - 1;i>0;i--){
int id = tempPath[i],idNext = tempPath[i-1];
tempCost += cost[id][idNext];
}
if(tempCost < minCost){
minCost = tempCost;
path = tempPath;
}
tempPath.pop_back();
return ;
}
tempPath.push_back(v);
for(int i=0;i<pre[v].size();i++){
DFS(pre[v][i]);
}
tempPath.pop_back();
}
int main(){
scanf("%d %d %d %d",&n,&m,&st,&ed);
int u,v;
fill(G[0],G[0] +MAXV *MAXV ,INF);
fill(cost[0],cost[0] +MAXV * MAXV,INF);
for(int i=0;i<m;i++){
scanf("%d %d",&u,&v);
scanf("%d %d",&G[u][v],&cost[u][v]);
G[v][u] = G[u][v];
cost[v][u] = cost[u][v];
}
Dijkstra(st);
DFS(ed);
for(int i=path.size()-1;i>=0;i--){
printf("%d ",path[i]);
}
printf("%d %d\n",d[ed],minCost);
return 0;
}
//pat a1003 bellman 算法
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
const int MAXV = 510;
const int INF = 0x3fffffff;
struct Node{
int v,dis;
Node(int _v,int _dis) : v(_v) , dis(_dis){ //构造函数
}
};
vector<Node> Adj[MAXV]; //图G的邻接表
// n 为顶点数,m为边数,st和ed 分别为起点和终点,weight【】 记录点权
int n,m,st,ed,weight[MAXV];
//d[] 记录最短距离,w[]记录最大点权之和,num[]记录最短路径数
int d[MAXV],w[MAXV],num[MAXV];
set<int> pre[MAXV]; //前驱
void Bellman(int s){
fill(d,d+MAXV,INF);
memset(num, 0,sizeof(num));
memset(w,0,sizeof(w));
d[s] = 0;
w[s] = weight[s];
num[s] = 1;
//以下为求解数组d的部分
for(int i=0;i<n-1;i++){
for(int u =0;u<n;u++){
for(int j=0;j<Adj[u].size();j++){
int v = Adj[u][j].v;
int dis = Adj[u][j].dis;
if(d[u] + dis < d[v]){
d[v] = d[u] +dis;
w[v] = w[u] + weight[v];
num[v] = num[u];
pre[v].clear();
pre[v].insert(u);
}else if( d[u] + dis == d[v]){
if(w[u] + weight[v] > w[v]){
w[v] = w[u] + weight[v];
}
pre[v].insert(u);
num[v] = 0;
set<int>::iterator it;
for(it = pre[v].begin();it != pre[v].end();it++){
num[v] +=num[*it];
}
}
}
}
}
}
int main(){
scanf("%d %d %d",&n,&m,&st,&ed);
for(int i=0;i<n;i++){
scanf("%d",&weight[i]); // 读入点权
}
int u,v,wt;
for(int i=0;i<m;i++){
scanf("%d %d %d",&u,&v,&wt);
Adj[u].push_back(Node(v,wt));
Adj[u].push_back(Node(u,wt));
}
Bellman(st);
printf("%d %d\n",num[ed],w[ed]);
return 0;
}
// floyd 解决全源最短路径问题
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 100000000;
const int MAXV = 200;
int n,m; //n为顶点数,m为边数
int dis[MAXV][MAXV]; //dis[i][j]表示顶点i j之间最短距离
void floyd(){
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(dis[i][k] !=INF && dis[k][j] !=INF && dis[i][k] +dis[k][j] < dis[i][j]){
dis[i][j] = dis[i][k] +dis[k][j];
}
}
}
}
}
int main(){
int u,v,w;
fill(dis[0],dis[0]+MAXV*MAXV,INF);
scanf("%d %d",&n,&m); //
for(int i=0;i<n;i++){
dis[i][i] = 0;
}
for(int i=0;i<m;i++){
scanf("%d %d %d",&u,&v,&w);
dis[u][v] = w;//以有向图为例进行过输入
}
floyd();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%d ",dis[i][j]);
}
printf("\n");
}
return 0;
}
/*
http://codeup.cn/problem.php?cid=100000621&pid=0
题目描述
在带权有向图G中,给定一个源点v,求从v到G中的其余各顶点的最短路径问题,叫做单源点的最短路径问题。
在常用的单源点最短路径算法中,迪杰斯特拉算法是最为常用的一种,是一种按照路径长度递增的次序产生最短路径的算法。
可将迪杰斯特拉算法描述如下:
在本题中,读入一个有向图的带权邻接矩阵(即数组表示),建立有向图并按照以上描述中的算法求出源点至每一个其它顶点的最短路径长度。
输入
输入的第一行包含2个正整数n和s,表示图中共有n个顶点,且源点为s。其中n不超过50,s小于n。
以后的n行中每行有n个用空格隔开的整数。对于第i行的第j个整数,如果大于0,则表示第i个顶点有指向第j个顶点的有向边,且权值为对应的整数值;如果这个整数为0,则表示没有i指向j的有向边。当i和j相等的时候,保证对应的整数为0。
输出
只有一行,共有n-1个整数,表示源点至其它每一个顶点的最短路径长度。如果不存在从源点至相应顶点的路径,输出-1。
请注意行尾输出换行。
样例输入 Copy
4 1
0 3 0 1
0 0 4 0
2 0 0 0
0 0 1 0
样例输出 Copy
6 4 7
提示
在本题中,需要按照题目描述中的算法完成迪杰斯特拉算法,并在计算最短路径的过程中将每个顶点是否可达记录下来,直到求出每个可达顶点的最短路径之后,算法才能够结束。
迪杰斯特拉算法的特点是按照路径长度递增的顺序,依次添加下一条长度最短的边,从而不断构造出相应顶点的最短路径。
另外需要注意的是,在本题中为了更方便的表示顶点间的不可达状态,可以使用一个十分大的值作为标记。
*/
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 510;
const int inf = 0x3fffffff;
int n,s,m;
int G[maxn][maxn];
bool vis[maxn] = {false};
int d[51];
void dijkstra(int s){
fill(d,d+maxn,inf);
d[s] = 0;
for(int i=0;i<n;i++){