Cod sursa(job #781004)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 22 august 2012 23:12:46
Problema Critice Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include<fstream>
#include<cstring>
using namespace std;
typedef struct celula{
           short nod;
           celula *next;
           }*lista;
typedef struct q{short x,y;}tip;
tip muchie[10001];
lista graf[1001],v,q,aux;
int cost[1001][1001],tata[1001],n,m,sol[10001],k,s[1001],d[1001];
bool ok,viz[1001];

inline void update(){
     int nod=n,min=1000000;
     while (tata[nod]!=-1){
           if (cost[tata[nod]][nod]<min) min=cost[tata[nod]][nod];
           nod=tata[nod];
           }
     nod=n;
     while (tata[nod]!=-1){
           cost[tata[nod]][nod]-=min;
           cost[nod][tata[nod]]+=min;
           nod=tata[nod];
           }
}

inline void bfs(){
     v=new celula; v->nod=1; v->next=0; q=v; tata[1]=-1; ok=0;
      memset(viz,0,sizeof(viz)); viz[1]=1;
     while (v!=0) {
           for (lista p=graf[v->nod];p; p=p->next){
               if (viz[p->nod]==0&&cost[v->nod][p->nod]>0) { if (p->nod!=n) {aux=new celula; aux->nod=p->nod; q->next=aux; q=aux; q->next=0;} tata[p->nod]=v->nod; viz[p->nod]=1;}
                if (viz[n]==1){ok=1; update(); viz[n]=0; }
                }
           v=v->next;
     }
}

void dfs(short nod,short caz){
     for (lista p=graf[nod];p; p=p->next)
      if (caz==1&&s[p->nod]==0&&cost[nod][p->nod]>0){ s[p->nod]=1; dfs(p->nod,caz); }
      else if (caz==n&&d[p->nod]==0&&cost[nod][p->nod]>0) { d[p->nod]=1; dfs(p->nod,caz); }
}

int main(void){
    ifstream fin("citrice.in");
    ofstream fout("citrice.out");
    int i,x,c,y; fin>>n>>m;
    for (i=1; i<=m; ++i) {
        fin>>x>>y>>c; cost[x][y]=cost[y][x]=c; muchie[i].x=x; muchie[i].y=y;
        v=new celula; v->nod=y; v->next=graf[x]; graf[x]=v;
        v=new celula; v->nod=x; v->next=graf[y]; graf[y]=v;
        }
    ok=1; while (ok==1) bfs();
     s[1]=1; dfs(1,1); d[n]=1; dfs(n,n);
    for (i=1; i<=m; ++i){
        x=muchie[i].x; y=muchie[i].y;
        if ( (cost[x][y]==0||cost[y][x]==0)&&( (d[x]>0&&s[y]>0)||(d[y]>0&&s[x]>0) ) ) {++k; sol[k]=i;}
        }
    fout<<k<<"\n";
    for (i=1; i<=k; ++i) fout<<sol[i]<<"\n";    
 return(0);
}