| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:幽幽道~~~还是数组开小了~~~~In Reply To:幽幽道~~~还是数组开小了~~~~ Posted by:eche at 2012-03-05 21:55:54 > 水题陷阱多。一开始数组开的512,WA了2次,反复检查算法无误,把Discuss里的数据一组组测,结果一个output是49的测出来是51,其他都对,纠结N久找不出原因,结果羞愤中把数组开到1000,奇迹般地跳正确结果49了,赶紧提交终于0msAC……经实验可以至少要开到560才行……
找到原因了,最大流每一步求出增长路径后,都要添加一条相应反向路径,我忘把这些反向路径的开销考虑进去,导致边数组开小了(我用的是游标数组实现的邻接链表表示法)。用邻接表的优势是占用内存少,遍历相邻边效率更高,注意点是添加边的时候一定要检查该边是否已存在,若存在仅更新边上的权即可。
附个AC代码供参考,因为作练习用,所以什么都自己写了,显得繁琐了
#include <stdio.h>
#include <string.h>
#define INFINITY 65535
#define STRLEN 25
#define ARCNUM 600
#define VNUM 600
#define HASHSIZE 1207
int n,m,k;
char str1[STRLEN],str2[STRLEN];
//Graph
struct ArcNode
{
int adjvex;
int cost;
int next;
}arcs[ARCNUM];
struct VNode
{
int firstarc;
}nodes[VNUM];
void InitArcs()
{
int i;
for(i=0;i<ARCNUM-1;++i)
arcs[i].next = i+1;
arcs[i].next = 0;
}
void InitNodes()
{
for( int i = 0; i<VNUM; ++i )
nodes[i].firstarc = 0;
}
int CursorAlloc()
{
int p;
p = arcs[0].next;
arcs[0].next = arcs[p].next;
return p;
}
void CursorFree(int p)
{
arcs[p].next = arcs[0].next;
arcs[0].next = p;
}
int InsertArc(int from, int to, int cost)
{
int p = CursorAlloc();
arcs[p].adjvex = to;
arcs[p].cost = cost;
arcs[p].next = nodes[from].firstarc;
nodes[from].firstarc = p;
return p;
}
int FindArc(int from, int to)
{
int p;
for( p = nodes[from].firstarc; p != 0 && arcs[p].adjvex != to; p = arcs[p].next );
return p;
}
//Hash
int NodeID;
struct HashEntry
{
char nodename[STRLEN];
int id;
bool filled;
}hashtable[HASHSIZE];
void InitHash()
{
for(int i=0;i<HASHSIZE;++i)
hashtable[i].filled = false;
NodeID = 3; // 1:start 2:end
}
unsigned int Hash(char* key)
{
unsigned int hash_v = 0;
while(*key!='\0')
hash_v += (hash_v<<5)+*key++;
return hash_v % HASHSIZE;
}
unsigned int Find(char* key)
{
unsigned int pos,collisionNum;
pos = Hash(key);
collisionNum = 0;
while( hashtable[pos].filled && strcmp(hashtable[pos].nodename,key) != 0 )
{
pos += 2*++collisionNum-1;
if(pos>=HASHSIZE)
pos-=HASHSIZE;
}
return pos;
}
void Insert(char* key)
{
unsigned int pos = Find(key);
if( !hashtable[pos].filled )
{
strcpy(hashtable[pos].nodename, key);
hashtable[pos].id = NodeID++;
hashtable[pos].filled = true;
}
}
//Queue
int queue[VNUM];
int front,rear,size;
void InitQueue()
{
front = rear = size = 0;
}
bool isEmpty()
{
return size == 0;
}
bool isFull()
{
return size == VNUM;
}
void Enqueue(int x)
{
if(isFull())
return;
size++;
queue[rear++] = x;
if( rear == VNUM )
rear = 0;
}
int Dequeue()
{
if(isEmpty())
return 0;
size--;
int rslt = queue[front++];
if( front == VNUM )
front = 0;
return rslt;
}
//ShortestPath
int dist[VNUM]; //到达该点最短路径的边最小花费
int from[VNUM];
void InitShortestPath()
{
for(int i = 0; i< VNUM; ++i)
{
dist[i] = INFINITY;
from[i] = 0;
}
}
bool ShortestPath()
{
int p,q;
InitShortestPath();
InitQueue();
dist[1] = 0;
Enqueue(1);
while(!isEmpty())
{
p = Dequeue();
for( q = nodes[p].firstarc; q != 0; q = arcs[q].next )
{
if( arcs[q].cost != 0 && dist[arcs[q].adjvex] == INFINITY )
{
if( p == 1 )
dist[arcs[q].adjvex] = arcs[q].cost;
else
dist[arcs[q].adjvex] = arcs[q].cost < dist[p] ? arcs[q].cost : dist[p];
from[arcs[q].adjvex] = p;
Enqueue(arcs[q].adjvex);
}
}
}
if( from[2] != 0 )
return true;
else
return false;
}
//MaxStream
int MaxStream()
{
int p, q;
int curStream,totalStream = 0;
int arc;
while( ShortestPath() )
{
curStream = dist[2];
totalStream += dist[2];
for( q = 2, p = from[q]; p != 0; q = p, p = from[q] )
{
arc = FindArc(p,q);
arcs[arc].cost -= curStream;
arc = FindArc(q,p);
if(!arc)
arc = InsertArc(q,p,0);
arcs[arc].cost += curStream;
}
}
return totalStream;
}
int main()
{
//freopen("D:\input.txt","r",stdin);
int i;
int from,to;
int arc;
InitHash();
InitArcs();
InitNodes();
scanf("%d",&n);
for(i=0;i!=n;++i)
{
scanf("%s",str1);
Insert(str1);
from = hashtable[Find(str1)].id;
arc = FindArc(from,2);
if(!arc)
arc = InsertArc(from,2,0);
arcs[arc].cost++;
}
scanf("%d",&m);
for(i=0;i!=m;++i)
{
scanf("%s%s",str1,str2);
from = Find(str1);
if( !hashtable[from].filled )
Insert(str1);
from = hashtable[from].id;
to = Find(str2);
if( !hashtable[to].filled )
Insert(str2);
to = hashtable[to].id;
arc = FindArc(1,from);
if(!arc)
arc = InsertArc(1,from,0);
arcs[arc].cost++;
arc = FindArc(from,to);
if(!arc)
arc = InsertArc(from,to,0);
arcs[arc].cost++;
}
scanf("%d",&k);
for(i=0;i!=k;++i)
{
scanf("%s%s",str1,str2);
from = Find(str1);
if( !hashtable[from].filled )
Insert(str1);
from = hashtable[from].id;
to = Find(str2);
if( !hashtable[to].filled )
Insert(str2);
to = hashtable[to].id;
arc = FindArc(from,to);
if(!arc)
InsertArc(from,to,INFINITY);
}
printf( "%d\n", m - MaxStream() );
//fclose(stdin);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator