Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXN 100010
  4.  
  5.  
  6. int inserir(int atual, int arestas, FILE* arq);
  7.  
  8. typedef struct node {
  9. int num;
  10. int peso;
  11. node* next;
  12. node* head;
  13. }node;
  14.  
  15. int N;
  16. node grafo[MAXN];
  17.  
  18. int main(){
  19. FILE* arq;
  20. int n;
  21.  
  22. arq=fopen("input1.txt", "r");
  23. if(arq==NULL){
  24. return 0;
  25. }
  26. fscanf(arq, "%d", &N);
  27. for(int i=1;i<=N;i++){
  28. grafo[i].next=NULL;
  29. fscanf(arq, "%d",&n );
  30. inserir(i, n, arq);
  31. }
  32. fclose(arq);
  33. return 0;
  34. }
  35.  
  36. int inserir(int atual, int arestas, FILE* arq){
  37. int v, w;
  38. node* aux;
  39. node* last;
  40. for(int i=0;i<arestas;i++){
  41. fscanf(arq, "%d %d", &v, &w);
  42. last = grafo+atual;
  43. while(last->next!=NULL){
  44. last= last->next;
  45. }
  46. aux = (node*) malloc((int)sizeof(node));
  47. aux->num = v;
  48. aux->next = NULL;
  49. aux->peso = w;
  50. if(grafo[atual].next==NULL){
  51. grafo[atual].next=aux;
  52. }
  53. else{
  54. last=grafo[atual].next;
  55. while(last->next!=NULL){
  56. last= last->next;
  57. }
  58. last->next = aux;
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement