Update 3562_unfinished.cpp

This commit is contained in:
Kirigaya Kazuto 2016-10-12 10:20:59 +08:00 committed by GitHub
parent 91d68a4520
commit 197b491f4e

View File

@ -6,22 +6,23 @@ using namespace std;
#define INF 0x3f3f3f3f #define INF 0x3f3f3f3f
/** SPFA 单源最短路径算法 不支持负环*/ /** SPFA 单源最短路径算法 不支持负环*/
const int MAXN = 1005; #define MAXN 1005;
int d[MAXN];/// distance [ From S to ... ] int d[MAXN];/// distance [ From S to ... ]
int v[MAXN];/// visit bool v[MAXN];/// visit
int q[MAXN];/// 基于数组的队列(也可用queue等...) int q[MAXN];/// 基于数组的队列(也可用queue等...)
int mp[MAXN][MAXN]; /// mp[i][j] i<--> j is connected. int mp[MAXN][MAXN]; /// mp[i][j] i<--> j is connected.
int n;/// n is the number of max Point . int n,m;/// n is the number of max Point .
int path[MAXN]; int path[MAXN];
void spfa(int StartPoint) /// d[i] is the min distance from StartPoint to i ( Both >=1 ) void spfa(int StartPoint) /// d[i] is the min distance from StartPoint to i ( Both >=1 )
{ {
memset(d,0x3f,sizeof(int)*(n+1)); for(int i=0; i<=n; i++)
memset(v,0,sizeof(int)*(n+1)); {
/* if(mp[0][i]!=INF)path[i]=i;//Path Init
for(int i=1;i<MAXN;i++) d[i]=INF;
d[i]=INF,v[i]=0;*/ v[i]=0;
}
int cnt=0; int cnt=0;
q[cnt++]=StartPoint; q[cnt++]=StartPoint;
v[StartPoint]=1; v[StartPoint]=1;
@ -29,25 +30,26 @@ void spfa(int StartPoint) /// d[i] is the min distance from StartPoint to i ( Bo
while(cnt>0) while(cnt>0)
{ {
int c=q[--cnt]; int c=q[--cnt];
v[c]=0; //v[c]=0;
for(int i=0;i<=n;i++) for(int i=0;i<=n;i++)
{ {
/// Here : if your mp[i][j] use INF as infinite, then use mp[c][i]!=INF. /// Here : if your mp[i][j] use INF as infinite, then use mp[c][i]!=INF.
/// Or you may use mp[i][j]!=-1 && d[i] > d[c] + mp[c][i] /// Or you may use mp[i][j]!=-1 && d[i] > d[c] + mp[c][i]
if( mp[c][i]!=INF && d[i]>d[c]+mp[c][i] ) if( d[i]>d[c]+mp[c][i] )
{ {
d[i]=d[c]+mp[c][i]; d[i]=d[c]+mp[c][i];
//path[i]=min(path[i],path[c]); //path[i]=min(path[i],path[c]);
if(!v[i]) if(!v[i])
{ {
if(c) path[i]=path[c]; if(c) path[i]=path[c];
v[i]=1,q[cnt++]=i; v[i]==1;
q[cnt++]=i;
} }
} }
if(c&&d[i]==d[c]+mp[c][i]) if(d[i]==d[c]+mp[c][i])
{ {
path[i]=min(path[i],path[c]); if(c)path[i]=min(path[i],path[c]);
} }
} }
} }
@ -59,29 +61,23 @@ int main()
scanf("%d",&t); scanf("%d",&t);
while(t--) while(t--)
{ {
int m; scanf("%d%d",&n,&m);
scanf("%d %d",&n,&m);
if(!n&&!m) break; if(!n&&!m) break;
n=n+1; n=n+1;
for(int i=0;i<=n;i++) for(int i=0;i<=n;++i)
{ {
for(int j=0;j<=n;j++) for(int j=0;j<=i;++j)
{ {
if(i==j) mp[i][j]=0; if(i==j) mp[i][j]=0;
else mp[i][j]=INF; else mp[i][j]=mp[j][i]=INF;
} }
} }
int tu,tv,tw;
for(int i=0;i<m;i++) for(int i=0;i<m;i++)
{ {
int u,v,w; scanf("%d%d%d",&tu,&tv,&tw);
scanf("%d %d %d",&u,&v,&w); mp[tu][tv]=tw;
mp[u][v]=w;
}
for(int i=0; i<=n; i++)
{
if(mp[0][i]!=INF)path[i]=i;//Path Init
} }
spfa(0); spfa(0);