锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
Total Submission: 1 Accepted: 1
The cows want to gather in the same pasture for their picnic, but (because of the one-way paths) some cows may only be able to get to some pastures. Help the cows out by figuring out how many pastures are reachable by all cows, and hence are possible picnic locations.
Lines 2..K+1: Line i+1 contains a single integer (1..N) which is the number of the pasture in which cow i is grazing.
Lines K+2..M+K+1: Each line contains two space-separated integers, respectively A and B (both 1..N and A != B), representing a one-way path from pasture A to pasture B.
2
3
1 2
1 4
2 3
3 4
緇撴灉灝辨槸婊¤凍cnt[i]==K鐨勬暟閲忥紝鍗砳鐐規墍鏈夌殑鐗涢兘鍙互鍒拌揪銆?br>
鐢╯pfa姹傦紝spfa鍦ㄨ繖閲屼笉鏄眰鏈孌佃礬寰勶紝鍙鍒頒簡灝辮錛屼笉闇瑕佹槸鏈鐭殑錛屽洜姝や細鏇村揩涓鐐廣?br>
#include<time.h>
#include<vector>
#include<queue>
using namespace std;
const int MAX=1001,INF=0x0fffffff;
vector<int> mp[MAX];
int d[MAX], cnt[MAX];
int K,N,M;
int stay[101];
void spfa(int x)
{
for(int i=1; i<=N; i++)
d[i]=INF;
queue<int>q;
q.push(x);
d[x]=0;
while(q.size())
{
int u=q.front(); q.pop();
for(int i=0; i<mp[u].size(); i++)
{
if(d[mp[u][i]]==INF)
{
d[mp[u][i]]=d[u]+1;
q.push(mp[u][i]);
}
}
}
}
int main()
{
cin>>K>>N>>M;
for(int i=1; i<=K; i++)
cin>>stay[i];
for(int i=1,s,t; i<=M; i++)
{
cin>>s>>t;
mp[s].push_back(t);
}
for(int i=1; i<=K; i++)
{
spfa(stay[i]);
for(int i=1; i<=N; i++)
if(d[i]!=INF)cnt[i]++;
}
int ans=0;
for(int i=1; i<=N; i++)
if(cnt[i]==K)ans++;
cout<<ans<<endl;
system("pause");
return 0;
}
]]>
浜屽垎鍥劇殑鏈澶у尮閰嶏紝鍔犱竴涓簮鐐癸紝榪欎釜婧愮偣鍒板乏杈規墍鏈夐《鐐瑰姞涓鏉¤竟錛屾潈鍊間負1. 銆傚悓鐞嗭紝鍔犱竴涓粓鐐癸紝鎵鏈夊彸杈圭殑瀹氱偣鍒拌繖涓粓鐐規湁鏉¤竟錛屾潈鍊間負1. 榪欐牱浜屽垎鍥劇殑鏈澶у尮閰嶅氨杞崲鎴愪簡鏈澶ф祦闂銆?/span>
#include<queue>
#include<cstring>
using namespace std;
const int MAX=405;
int cap[MAX][MAX]={0};
int flow[MAX][MAX]={0};
int pre[MAX],m[MAX];
int N, M,S,T;
const int INF=10000000;
int bfs(int s)
{
memset(m,0,sizeof m);
memset(pre,0,sizeof pre);
queue<int> q;
q.push(s);
m[s]=INF;
while(!q.empty())
{
int u=q.front(); q.pop();
for(int v=0; v<=N+M+1; v++)
if(!m[v]&&cap[u][v]>flow[u][v])
{
pre[v]=u;
m[v]= m[u]>cap[u][v]-flow[u][v]?cap[u][v]-flow[u][v]:m[u];
q.push(v);
}
}
if(m[T]==0)return 0;
for(int u=T; u!=S ; u=pre[u])
{
flow[pre[u]][u]+=m[T];
flow[u][pre[u]]-=m[T];
}
return m[T];
}
int main()
{
while(cin>>N>>M)
{ memset(cap,0,sizeof cap);
memset(flow,0,sizeof flow);
for(int i=1; i<=N; i++)
{
int c,e;
cin>>c;
for(int j=1; j<=c; j++)
{
cin>>e;
e=e+N;
cap[i][e]=1;
}
}
S=0;
T=N+M+1;
for(int i=1; i<=N; i++)
cap[0][i]=1;
for(int i=1; i<=M; i++)
cap[N+i][T]=1;
int ans=0;
while(1)
{
int temp=bfs(S);
if(temp==0)break;
else ans+=temp;
}
cout<<ans<<endl;
}
system("pause");
return 0;
}
]]>
璧よ8瑁哥殑鏈澶ф祦闂錛屼篃鏄垜鍐欑涓涓渶澶ф祦錛屾槸鍏ラ棬鐨勫ソ棰樸?br>
鍙戠幇鐪嬩唬鐮佹湁鏃跺欐瘮鐪嬩功鏇村鏄撴噦錛岀湅鏈澶ф祦宸茬粡鐪嬩簡濂藉嚑澶╀功浜嗭紝閮芥槸鏅曚箮涔庣殑錛屽弬鑰冧簡涓涓嬪埆浜虹殑浠g爜錛屼技涔庢湁浜涗簡瑙d簡銆?br>
Ford-Fulkerson鏂規硶鐨勫騫胯礬綆楁硶
姣忔鐢╠fs瀵繪壘鍙騫胯礬寰勶紝璇ヨ礬寰勪笂鐨勬墍鏈夎竟鐨勫閲忓潎瑕佹槸姝f暟錛屽鏋滄壘鍒板氨鍙栬璺緞涓婃墍鏈夊閲忕殑鏈灝忓間綔涓哄鍔犵殑嫻併傚鏋滀笉瀛樺湪鍒欒鏄庡凡緇忔槸鏈澶ф祦錛岀粨鏉熴?br>
涓嬮潰榪欎袱涓▼搴忓湪hdu鏈夌偣灝忛棶棰橈紝絎笁涓槸鍦ㄤ袱涓狾J涓婇兘鍙互鐨勩?br>
using namespace std;
int map[250][250];
int pre[250];
bool visit[250];
int m,n,minflow;
const int INF=0x7fffffff/1000;
int dfs(int v)
{
visit[v]=true;
if(v==n)return 1;
for(int i=1; i<=n; i++)
if(map[v][i]>0&&!visit[i])
{
pre[i]=v;
minflow=minflow>map[v][i]?map[v][i]:minflow;
if(dfs(i))return true;
}
return 0;
}
int maxflow()
{
int maxf=0;
while(1)
{
memset(pre,0,sizeof pre);
minflow=INF;
memset(visit,0,sizeof visit);
pre[1]=-1;
if(dfs(1))
{
int inc=minflow;
int t=n;
while(pre[t]!=-1)
{
int qq=pre[t];
map[qq][t]-=inc;
map[t][qq]+=inc;
t=qq;
}
maxf+=inc;
}
else break;
}
return maxf;
}
int main()
{
while(cin>>m>>n)
{
int s,t,c;
memset(map,0,sizeof map);
while(m--)
{
cin>>s>>t>>c;
map[s][t]+=c;
}
cout<<maxflow()<<endl;
}
system("pause");
return 0;
}
鐢˙FS鐨凟dmonds-Karp綆楁硶
#include<queue>
using namespace std;
int map[250][250];
int pre[250];
bool visit[250];
int m,n,minflow;
const int INF=0x7fffffff/1000;
int bfs(int s)
{
queue<int> Q;
Q.push(s);
visit[s]=true;
while(Q.size())
{
int v=Q.front(); Q.pop();
if(v==n)return true ;
for(int i=1; i<=n; i++)
if(!visit[i]&&map[v][i]>0&&!visit[n])
{
pre[i]=v;
visit[i]=true;
minflow=minflow>map[v][i]?map[v][i]:minflow;
Q.push(i);
}
}
return false;
}
int maxflow()
{
int maxf=0;
while(1)
{
memset(pre,0,sizeof pre);
minflow=INF;
memset(visit,0,sizeof visit);
pre[1]=-1;
if(bfs(1))
{
int inc=minflow;
int t=n;
while(pre[t]!=-1)
{
int qq=pre[t];
map[qq][t]-=inc;
map[t][qq]+=inc;
t=qq;
}
maxf+=inc;
}
else break;
}
return maxf;
}
int main()
{
while(cin>>m>>n)
{
int s,t,c;
memset(map,0,sizeof map);
while(m--)
{
cin>>s>>t>>c;
map[s][t]+=c;
}
cout<<maxflow()<<endl;
}
system("pause");
return 0;
}
闂鎴栬灝卞嚭鐜板湪鍙栨渶灝忓肩殑闂涓婏紝涓嶈繃鎴戣涓哄墠涓ょ姣忔鍙栫殑鍙兘涓嶆槸鏈灝忕殑錛屼絾搴旇鏄病閿欑殑銆?br>鏈澶氬dfs鎴朾fs鍑犳銆?br>
#include<string.h>
using namespace std;
int map[250][250];
int pre[250],q[250];
bool visit[250];
int m,n;
const int INF=0x7fffffff;
bool dfs(int v)
{
visit[v]=true;
if(v==n)return true;
for(int i=1; i<=n; i++)
if(map[v][i]>0&&!visit[i])
{
pre[i]=v;
q[i]=q[v]>map[v][i]?map[v][i]:q[v];
if(dfs(i))return true;
}
return 0;
}
int maxflow()
{
int maxf=0;
while(1)
{
memset(pre,0,sizeof pre);
memset(q,0,sizeof q);
//for(int i=1; i<=n; i++)
q[1]=INF;
memset(visit,0,sizeof visit);
pre[1]=-1;
if(dfs(1))
{
int inc=q[n];
int t=n;
while(pre[t]!=-1)
{
int qq=pre[t];
map[qq][t]-=inc;
map[t][qq]+=inc;
t=qq;
}
maxf+=inc;
}
else break;
}
return maxf;
}
int main()
{
while(cin>>m>>n)
{
int s,t,c;
memset(map,0,sizeof map);
while(m--)
{
cin>>s>>t>>c;
map[s][t]+=c;
}
cout<<maxflow()<<endl;
}
system("pause");
return 0;
}
]]>
絎竴鍒╃敤dijstra姹傚嚭浠嶺鍒板悇鐐圭殑鏈鐭窛紱匯?br>
鐒跺悗鎵鏈夌殑杈瑰弽鍚戯紝鍐嶈繘琛屼竴嬈ijstra姹俋鍒板悇鐐圭殑鏈鐭礬寰勩?br>
絎簩嬈℃眰鍑虹殑鏈鐭礬寰勪篃灝辨槸鍚勭偣鍒癤鐨勬渶鐭礬寰勶紝鍥犱負杈瑰凡緇忓弽鍚戯紝瀵逛簬絎簩嬈′粠X鍒板悇鐐圭殑鏈鐭礬寰勬鏄?br>鍘熷浘浠庡悇鐐瑰埌X鐨勬渶鐭礬寰勩?br>
#include<queue>
using namespace std;
const int INF=0x7fffffff/100;
int map[1001][1001]={0};
int d[1001]={0},N,M,X;
int dd[1001]={0};
bool f[1001];
void dijstra()
{
for(int i=1; i<=N; i++)
d[i]=INF;
d[X]=0;
memset(f,0,sizeof f);
for(int i=1; i<=N; i++)
{
int min=INF,u=0;
for(int j=1; j<=N; j++)
if(d[j]<min&&!f[j])
{
min=d[j];
u=j;
}
f[u]=true;
for(int j=1; j<=N; j++)
{
if(d[u]+map[u][j]<d[j])
d[j]=d[u]+map[u][j];
}
}
}
void traverse()
{
int temp;
for(int i=1; i<=N; i++)
for(int j=1; j<i; j++)
{
temp = map[i][j];
map[i][j]=map[j][i];
map[j][i]=temp;
}
}
int main()
{
cin>>N>>M>>X;
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
map[i][j]=INF;
for(int i=1,s,t,value; i<=M; i++)
{
cin>>s>>t>>value;
map[s][t]=value;
}
dijstra();
for(int i=1; i<=N; i++)
dd[i]=d[i];
traverse();
dijstra();
int max=0;
for(int i=1; i<=N; i++)
if(d[i]+dd[i]>max)max=d[i]+dd[i];
cout<<max<<endl;
system("pause");
return 0;
}
]]>
Total Submission: 36 Accepted: 4
For each case,the number of positions ( 1 < = n < = 100) and number m of bridges are exhibited on the first line.The following m lines contain triples of integers specifying start and end positions of the bridge and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one bridge between each pair of crossings.
3 3
1 2 3
1 3 4
2 3 5
4[EOF]
棰樻剰錛?br>n涓偣錛屾湁浜涚偣闂存湁妗ワ紝妗ヤ笂鏈夋渶澶ф壙閲嶉噺錛岄棶浣犱粠1鍒皀鍙互鏈澶ф惡甯︾殑鐗╁搧鐨勯噸閲忋?br>棰樿В錛?nbsp;
Folyd綆楁硶鍙樺艦錛屾妸姹傛渶鐭礬寰勭殑鍜屾敼涓烘眰鏈澶ц澆閲嶉噺鐨勯棶棰橈紝鍏抽敭鏄痙is[i][j]=dis[i][j]>dis[i][k]+dis[k][j]?dis[i][j]>dis[i][k]+dis[k][j]?:dis[i][j];鎹㈡垚dis[i][j]=max(dis[i][j],min(dis[i][k],dis[k][j]));
#include<iostream>
2#include<cmath>
3#include<string.h>
4using namespace std;
5long long dis[105][105];
6
8void Floyd(int n)
9{
10 for(int k=1; k<=n; k++)
11 for(int i=1; i<=n; i++)
12 for(int j=1; j<=n; j++)
13
{
14 if(i!=k&&j!=k&&dis[i][k]&&dis[k][j])
15 dis[i][j]=max(dis[i][j],min(dis[i][k],dis[k][j]));
16 }
17}
18
19int main()
20{
21 int t,i,j,m,n;
22 cin>>t;
23 for(int k=1; k<=t; k++)
24
{
25 cin>>n>>m;
26 memset(dis,0,sizeof (dis));
27 i=1;
28 for(int s,e,w; i<=m; i++)
29
{
30 cin>>s>>e>>w;
31 dis[s][e]=dis[e][s]=w;
32 }
33
34 Floyd(n);
35
36 cout<<"Case #"<<k<<':'<<endl<<dis[1][n]<<endl<<endl;
37 }
38 return 0;
39}
]]>
Time Limit:JAVA/Others2000/1000MS Memory Limit:JAVA/Others131072/65536KB
Total Submit:6 Accepted:2
Description
Input
Line 1: | Integer P (1 <= P <= 10000) the number of paths that interconnect the pastures (and the barn) |
Line 2..P+1: | Space separated, two letters and an integer: the names of the interconnected pastures/barn and the distance between them (1 <= distance <= 1000) |
Output
Sample Input
5 A d 6 B d 3 C e 9 d Z 8 e Z 3
Sample Output
B 11